我正在使用 UINavigationController。我想显示一个中间屏幕,例如。白色,然后从那里我想放弃并转向绿色。
我不创建从白色到绿色的过渡的原因是,在用户返回的情况下,他们应该返回到蓝色,因为蓝色是我的主屏幕。
class BlueViewController: UIViewController {
@IBAction func tapBlue(_ sender: Any) {
self.performSegue(withIdentifier: "whiteSegue", sender: self)
}
}
class WhiteViewController: UIViewController {
@IBAction func tapGreen(_ sender: Any) {
navigationController?.popViewController(animated: true)
weak var pvc = self.presentingViewController
dismiss(animated: true){
pvc?.performSegue(withIdentifier: "greenSegue", sender: self)
}
}
}
这是代码库
https://github.com/omenking/DismissAndSegue
没有发生错误,但是当白色被消除时,它不会变成绿色。 我知道之前在 StackOverflow 上已经有人问过这个问题,但其他示例不起作用或者对于最新的 iOS 来说已经过时了。
最佳答案
主要问题是,由于您使用导航 View Controller 并将 View Controller 插入和推出堆栈,因此 self.presentingViewController
变量将为 nil。它用于模式演示,而不是导航 View Controller 。
试试这个:
class WhiteViewController: UIViewController {
@IBAction func tapGreen(_ sender: Any) {
// Get navigation stack, remove last item (White VC)
var viewControllers = navigationController?.viewControllers
viewControllers.removeLast()
// Instantiate new Green VC from storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil) //Change bundle name
let greenViewController = storyboard.instantiateViewController(withIdentifier: "GreenViewController") //Change storyboard ID
viewControllers.append(greenViewController)
// Perform the transition to Green VC with animation
navigationController?.setViewControllers(viewControllers, animated: true)
}
}
关于swift - View Controller 被关闭后执行segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49989151/