简体   繁体   中英

Go back to previous view controller doesn't work

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?

@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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM