本文介绍了当前模型关闭时间时快速传递数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景,我有 tabbar 和三个 viewcontroller 和第一个 tabbar 视图控制器我正在展示 tableview一些数据.每当我单击 tableview cell 时,我都会将数据传递给一个特殊的当前 model 弹出控制器.如果我 dismiss 弹出控制器我需要直接显示到标签栏 index 1(我的意思是标签栏第二个 viewcontroller)也需要传递 .

My scenario, I am having tabbar with three viewcontroller and first tabbar viewcontroller I am showing tableview with some data. Whenever I am clicking the tableview cell I am passing the data to one special present model popupcontroller. If I dismiss popup controller I need to show directly to tabbar index 1 (I mean tabbar second viewcontroller) also need to pass the values.

这里,我试过下面的代码

Here, below code I tried

  1. 选择tableview单元格后将值传递给popupview控制器
  2. 点击弹出关闭按钮将相同的值传递给标签栏索引 1(标签栏第二个视图控制器)

内部弹出视图控制器

var item : Datum! //Datum Codable Root

@IBAction func close_click(_ sender: Any) {
    NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSocket"), object: nil)
    if let presenter = presentingViewController as? HomeViewController {
           presenter.updatitem = item
    }
    dismiss(animated: true, completion: nil)
 }

一旦弹出 dismissed 显示 tabbar 第一个 viewcontroller(索引 0),所以我添加了 NotificationCenter 来调用函数并更改索引.

Once popup dismissed showing tabbar first viewcontroller (index 0), so I added NotificationCenter to call function and change the index.

推荐答案

首先你不需要使用 NotificationCenter 因为你可以简单地通过传递 UITabBarController当您在 tableView 中选择任何单元格时, 对象到下一个视图控制器,如下所示:

First of all you don't need to use NotificationCenter for that because you can simply achieve it by passing the UITabBarController object to next view controller when you select any cell in tableView like shown below:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    vc.selectedIndex = indexPath.row
    vc.tabBar = self.tabBarController
    self.present(vc, animated: true, completion: nil)
}

并且您需要将 var tabBar: UITabBarController? 声明到 DetailViewController 中,它将保存前一个视图控制器传递的值.

and you need to declare var tabBar: UITabBarController? into DetailViewController which will hold the value passed by previous view controller.

然后,当您关闭视图控制器时,您可以简单地使用 self.tabBar?.selectedIndex = 1 选择 UITabBarController 的另一个索引,然后您可以简单地将值传递给第一个索引视图控制器,如:

Then when you dismiss the view controller you can simply select the another index of UITabBarController with self.tabBar?.selectedIndex = 1 and you can simply pass the value to first index view controller like:

@IBAction func close_click(_ sender: Any) {
    dismiss(animated: true, completion: {

        if let secondTab = self.tabBar?.viewControllers?[1] as? SecondViewController {
            secondTab.selectedIndexFromFirstTab = self.selectedIndex
            secondTab.tfData = self.userTF.text!
        }
        self.tabBar?.selectedIndex = 1
    })
}

并且您需要在 SecondViewController 中声明对象,例如:

and you need to declare objects in SecondViewController like:

var selectedIndexFromFirstTab = 0
var tfData = ""

将保存先前视图的数据.

Which will hold the data of previous view.

现在您的结果将如下所示:

Now your result will look like:

有关更多信息,您可以参考这个演示项目.

For more info you can refer THIS demo project.

因此,从您的代码中,我看到您已将导航控制器嵌入到 tabBar 控制器中,因此您需要更新 close_click 方法,如下所示:

So from your code I have seen that you have embed in navigation controllers to your tabBar controllers so you need to update close_click method like shown below:

@IBAction func close_click(_ sender: Any) {
    dismiss(animated: true, completion: {

        if let navView = self.tabBar?.viewControllers?[1] as? UINavigationController {
            if let secondTab = navView.viewControllers[0] as? HomeViewController {
                secondTab.selectedIndexFromFirstTab = self.selectedIndex
                secondTab.item = self.item
                secondTab.tfData = "Sample"
            }
        }
        self.tabBar?.selectedIndex = 1
    })
}

这篇关于当前模型关闭时间时快速传递数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:17