gpt4 book ai didi

ios - 返回上一个 View Controller 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:38:52 25 4
gpt4 key购买 nike

我的第一个 View Controller 有一个按钮,它会触发 @IBAction goTo2ndVc(),它会呈现第二个 ViewController:

class FirstVC: UIViewController {
...
@IBAction func goTo2ndVc() {
let secondVc = SecondVC(label: "I am second vc.")
self.presentViewController(secondVc, animated: true, completion: nil)
}

按下按钮时,第二个 View Controller 显示在屏幕上。没问题。

在第二个 View Controller 中,还有一个用于返回到第一个 View Controller 的按钮:

class SecondVC: UIViewController {
...
@IBAction func backToFirst(sender: AnyObject) {
print("go back ...")
self.navigationController?.popViewControllerAnimated(true)
}
}

我在网上看过,人们建议使用 navigationController?.popViewControllerAnimated(true) 返回到之前的 Controller 。但是当我按下返回按钮时,我可以看到打印消息“返回...”但是应用程序不会返回到第一个 View Controller 。为什么?

最佳答案

@IBAction func backToFirst(sender: AnyObject) {
print("go back ...")
self.dismissViewControllerAnimated(true, completion: nil)
}

在 swift 3 中

self.dismiss(animated: true, completion: nil)

你不应该使用导航 Controller ,因为你在添加第二个 View Controller 时没有使用它。这就是为什么只需调用 dismissViewControllerAnimated 方法。

仅当您通过 pushViewController 方法添加 View Controller 时,您才必须使用 UINavigationController 及其 pop 方法。

在这里熟悉导航 Controller 的概念:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html

关于ios - 返回上一个 View Controller 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35814405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com