【问题标题】:whats the correct way to be back to top view controller in the navigation controller stack? [duplicate]在导航控制器堆栈中返回顶部视图控制器的正确方法是什么? [复制]
【发布时间】:2018-02-16 22:24:20
【问题描述】:

https://i.stack.imgur.com/ayMVR.png

对不起,我是初学者,我担心以后做错了会做出什么奇怪的事情。

如果我有 3 个如上图所示的视图控制器,并且在第 3 个视图控制器中,我有一个警报,在按下警报后将我发送回第一个视图控制器,如下面的代码:

class ViewController3: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let alert = UIAlertController(title: "Great", message: "lets get back to VC1", preferredStyle: .alert)
        let alertAction1 = UIAlertAction(title: "back", style: .default) { (action) in


            self.navigationController?.popToRootViewController(animated: true)


        }

        alert.addAction(alertAction1)
        present(alert, animated: true, completion: nil)


    }


}

有两种方法可以返回到第一个视图控制器,通过使用

1. self.navigationController?.popToRootViewController(animated: true)

或使用 2. perform segue with identifier ,返回第一个视图控制器的正确方法是什么?第一个还是第二个?为什么?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    最好的办法是

      1. self.navigationController?.popToRootViewController(animated: true)
    

    还要检查this,因为你会明白为什么它更好(清除导航堆栈)

    【讨论】:

      【解决方案2】:

      这是正确的方法

      1. self.navigationController?.popToRootViewController(animated: true)
      

      segue 将使它们都留在堆栈中

      【讨论】:

      • 当然,segue 只会在顶部放置一个新控制器。
      猜你喜欢
      • 2015-05-17
      • 2019-03-14
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多