【发布时间】:2020-05-22 15:18:52
【问题描述】:
我知道关于这个主题的问题和答案很少,但是当我说我在发布这个问题之前已经检查过它们时相信我。
我正在尝试将信息传递给控制器(显示的前一个控制器)。总而言之,我正在尝试将信息从第二个控制器 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")
}
}
【问题讨论】:
-
您只想将信息传递给您弹出的控制器吗?
标签: swift xcode navigation viewcontroller