Ios 当前模型解除时快速传递数组值
在我的场景中,我使用三个Ios 当前模型解除时快速传递数组值,ios,swift,uitabbarcontroller,Ios,Swift,Uitabbarcontroller,在我的场景中,我使用三个viewcontroller和第一个tabbarviewcontroller来显示tableview和一些数据。每当我单击表格视图单元格时,我都会将数据传递给一个特殊的礼物型号popupcontroller。如果我关闭弹出式控制器,我需要直接显示到tabbar索引1(我指的是tabbar secondviewcontroller),还需要传递值 下面是我尝试过的代码 选择将值传递给popupview控制器的tableview单元格后 单击弹出关闭按钮,将相同的值传递给选项
viewcontroller
和第一个tabbar
viewcontroller来显示tableview
和一些数据。每当我单击表格视图
单元格
时,我都会将数据传递给一个特殊的礼物型号
popupcontroller。如果我关闭弹出式控制器,我需要直接显示到tabbar索引1
(我指的是tabbar secondviewcontroller
),还需要传递值
下面是我尝试过的代码
选择将值传递给popupview控制器的tableview单元格后
单击弹出关闭按钮,将相同的值传递给选项卡栏索引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)
}
一旦弹出disabled
显示tabbar
第一个viewcontroller
(索引0),我添加了NotificationCenter
来调用函数并更改索引 您可以使用dismise方法的完成处理程序
dismiss(animated: true, completion: nil)
传递一个方法将选项卡栏更改为第二个选项卡,而不是传递nil
解除视图控制器的解除时,将调用解除视图控制器中的完成处理程序
viewController.dismissViewControllerAnimated(true, completion: {
FunctionToChangeToSecondTab()
})
首先,您不需要使用NotificationCenter
,因为在tableView
中选择任何单元格时,只需将UITabBarController
对象传递给下一个视图控制器,即可实现此目的,如下所示:
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)
}
@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
})
}
您需要将var tabBar:UITabBarController?
声明到DetailViewController
中,它将保存上一个视图控制器传递的值
然后,当您关闭视图控制器时,您只需使用self.tabBar?选择UITabBarController
的另一个索引。selectedIndex=1
,您只需将该值传递给第一个索引视图控制器,如下所示:
@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
中声明对象,如:
var selectedIndexFromFirstTab = 0
var tfData = ""
它将保存上一个视图的数据
现在,您的结果将如下所示:
有关更多信息,您可以参考演示项目
编辑:
因此,从您的代码中,我看到您已将导航控制器嵌入到选项卡
控制器中,因此您需要更新关闭\u单击
方法,如下所示:
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)
}
@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
})
}
谢谢你@Dharmesh Kheni。我会检查和更新这里有没有可能隐藏底部选项卡栏一旦我达到第二个VC@你看过我的演示项目了吗?对不起。现在我正在检查@达尔梅什·凯尼苏尔:好的。这将有助于你理解这里的逻辑。