解散模式vc swift之后的popviewcontroller

发布于 2025-02-10 03:18:48 字数 725 浏览 1 评论 0原文

我想在解散modalvc之后popviewController。但是我的代码不起作用。怎么了?

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)

I want to popViewController after I dismiss a modalVC. However my code is not working. What is wrong?

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)
    }

I have also tried this:

  let controller = ViewController()             
controller?.popViewController(animated: true)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-02-17 03:18:48

我创建了两个ViewControllers VC1和VC2。 VC1具有导航控制器,并且VC2在VC1顶部推动是导航堆栈。在VC2中,显示了警报,并在OK按钮的点击中弹出并从导航堆栈中删除。对我来说很好。
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)
}

I created two viewcontrollers vc1 and vc2. vc1 has a navigation controller and vc2 is pushed on top of vc1 is the navigation stack. In vc2 the alert is shown and on tap of ok button vc2 is popped and removed from navigation stack. Works fine for me.
Code for vc1:

class ViewController: UIViewController {

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

Code for 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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文