我想在我解雇了一个popViewController后,再去做modalVC。但是,我的代码不起作用。怎么啦?
func showMessage(withTitle title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "ok", style: .default) { (_) in
self.dismiss(animated: true) {
self.navigationController?.popViewController(animated: true)
}
}
alert.addAction(alertAction)
present(alert, animated: true, completion: nil)
}我也尝试过这样做:
let controller = ViewController()
controller?.popViewController(animated: true)发布于 2022-06-27 04:37:40
我创建了两个视图控制器vc1和vc2。vc1有一个导航控制器,vc2被推到vc1之上,是导航堆栈。在vc2中,会显示警报,在单击ok按钮时,vc2被弹出并从导航堆栈中删除。对我来说很好。Vc1代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.pushViewController(storyboard?.instantiateViewController(withIdentifier: "SecViewController") as? SecViewController ?? SecViewController(), animated: true)
}
}Vc2代码:
class SecViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
showMessage(withTitle: "title", message: "Message")
}
func showMessage(withTitle title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "ok", style: .default) { (_) in
self.dismiss(animated: true) {
self.navigationController?.popViewController(animated: true)
}
}
alert.addAction(alertAction)
present(alert, animated: true, completion: nil)
}https://stackoverflow.com/questions/72715742
复制相似问题