首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >popViewController解散模态VC Swift后

popViewController解散模态VC Swift后
EN

Stack Overflow用户
提问于 2022-06-22 12:42:41
回答 1查看 45关注 0票数 0

我想在我解雇了一个popViewController后,再去做modalVC。但是,我的代码不起作用。怎么啦?

代码语言:javascript
复制
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)
    }

我也尝试过这样做:

代码语言:javascript
复制
  let controller = ViewController()             
controller?.popViewController(animated: true)
EN

回答 1

Stack Overflow用户

发布于 2022-06-27 04:37:40

我创建了两个视图控制器vc1和vc2。vc1有一个导航控制器,vc2被推到vc1之上,是导航堆栈。在vc2中,会显示警报,在单击ok按钮时,vc2被弹出并从导航堆栈中删除。对我来说很好。Vc1代码:

代码语言:javascript
复制
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
  self.navigationController?.pushViewController(storyboard?.instantiateViewController(withIdentifier: "SecViewController") as? SecViewController ?? SecViewController(), animated: true)
}
}

Vc2代码:

代码语言:javascript
复制
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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72715742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档