Go back to previous view controller doesn't work

末鹿安然 提交于 2020-01-11 06:45:14

问题


My first view controller has a button, which triggers the @IBAction goTo2ndVc() which presents a second ViewController:

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

When the button is pressed, the 2nd view controller is shown on screen. No problem.

In 2nd view controller, there is also a button which is used to go back to 1st view controller:

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

I looked on internet, people suggest to use navigationController?.popViewControllerAnimated(true) to go back to previous controller. But when I press the go back button I can see the print message "go back ..." but the app doesn't go back to 1st view controller. WHY?


回答1:


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

In Swift 3

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

you should not use navigation controller, because you didn't use it when you were adding the second view controller. that's why simply call dismissViewControllerAnimated method.

You have to use UINavigationController and its pop methods only when you add your view controllers via pushViewController method.

Familiarize yourself with the concept of navigation controller here: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html




回答2:


there

the issue is very simple..

self.presentViewController(secondVc, animated: true, completion: nil)

the code will present second view, you are not pushing it.

self.navigationController?.popViewControllerAnimated(true)

the popViewController will pop back to the previous view controller from where it is been pushed. So, there are two ways you can achieve what you want

1)If you want to present viewController then you have to dismiss the view controller to show previous view controller with

self.dismissViewControllerAnimated(true, completion: nil)

2)If you want to use PopToVewcontroller, then you have to push you second view controller instead of presenting it with

self.navigatioVonroller?.pushViewController(secondVc, animated: true)



回答3:


If you want to return to the previous view controller, you can simply add:

[self dismissViewControllerAnimated:YES completion:nil];

to the button action method.

If this is added on the nav view controller present on every screen, I see no reason why it shouldn't work as it would always dismiss the most recently presented view.



来源:https://stackoverflow.com/questions/35814405/go-back-to-previous-view-controller-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!