我知道关于该主题的问题和答案很少,但是当我说在发布此问题之前已经检查它们时,请相信我。
我正在尝试将信息传递给控制器(显示的是前一个)。总而言之,我试图将信息从第二个控制器发送SearchViewController
到第一个控制器MainViewController
(嵌入在导航控制器中)。
我尝试了几种方法来实现,但是没有人正是我想要的。我将粘贴它们中的每一个以及问题的说明:
注意
请注意,我避免使用segue
,因此这不是一种选择。
尝试1
导航正确完成。正确的是,它以全屏显示,而不是弹出窗口显示。但是,它并不会保留所做的更改coordinate
,而是采用默认值,这使我相信它是一个新实例,而不是我尝试导航的实例。
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Full screen. Navigation controller shown. Does not keep the changes in variables
let vc = MainViewController()
vc.setCoordinate(coordinate: self.coordinate!)
navigationController?.popToRootViewController(animated: true)
} else {
showToast(message: "Please, enter the coordinate")
}
}
尝试2导航正确完成,并保持所做的更改。coordinate.
尽管如此,它显示为弹出窗口,并且不再显示导航控制器。
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Pop window. Not full screen. Navigation controller not shown. Keeps the variable values
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
mainViewController.setCoordinate(fcoordinate: coordinate!)
self.present(mainViewController, animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
尝试3
我之所以使用这种方法,是因为我试图回到以前的控制器,但是dismiss
没有导航到任何地方,所以它失败了。
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Does not come back to the previous controller
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
mainViewController.setCoordinate(coordinate: coordinate!)
self.dismiss(animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
尝试4
这种方法不允许我访问的方法/变量,mainViewController
因此无法更改的值coordinate
。此外,该ViewController
图标显示为弹出窗口而不是全屏显示
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Pop window. Not full screen. No access to methods
guard let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainViewControllerID") else { return }
let navController = UINavigationController(rootViewController: vc)
self.navigationController?.present(navController, animated: true, completion: nil)
} else {
showToast(message: "Please, enter the coordinate")
}
}
尝试5
导航正确。全屏显示,很好,但是未显示导航控制器。它不会保留所做的更改coordinate
@IBAction func setCoordinate(_ sender: UIBarButtonItem) {
// Set the new values of from/to coordinates and navigates to the main controller
if((self.coordinate) {
// Full screen. Navigation controller shown. Does not keep the changes in variables
let vc = MainViewController()
vc.setCoordinate(coordinate: self.coordinate!)
self.navigationController?.popViewController(animated: true)
} else {
showToast(message: "Please, enter the coordinate")
}
}
您所有的尝试都将实例化一个新的vc,然后再用标记为let vc = MainViewController()
or 的实际呈现的vc进行实例化,storyBoard.instantiateViewController
因此您需要
let arr = self.navigationController!.viewControllers
let prev = arr[arr.count - 2] as! MainViewController
prev.setCoordinate(coordinate: self.coordinate!)
self.navigationController!.popToRootViewController(animated: true)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句