Go back to previous view controller doesn't work

后端 未结 3 1830
名媛妹妹
名媛妹妹 2021-01-14 22:18

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

class FirstVC: UIViewCon         


        
相关标签:
3条回答
  • 2021-01-14 22:35
    @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

    0 讨论(0)
  • 2021-01-14 22:43

    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)
    
    0 讨论(0)
  • 2021-01-14 22:53

    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.

    0 讨论(0)
提交回复
热议问题