Programmatically go back to previous ViewController in Swift

前端 未结 15 1525
野的像风
野的像风 2020-12-04 04:50

I send the user over to a page on a button click. This page is a UITableViewController.

Now if the user taps on a cell, I would like to push him ba

相关标签:
15条回答
  • 2020-12-04 05:54

    Swift 3

    I might be late in the answer but for swift 3 you can do it this way:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< Back", style: .plain, target: self, action: #selector(backAction))
    
        // Do any additional setup if required.
    }
    
    func backAction(){
        //print("Back Button Clicked")
        dismiss(animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-12-04 05:54

    Swift 4

    there's two ways to return/back to the previous ViewController :

    1. First case : if you used : self.navigationController?.pushViewController(yourViewController, animated: true) in this case you need to use self.navigationController?.popViewController(animated: true)
    2. Second case : if you used : self.present(yourViewController, animated: true, completion: nil) in this case you need to use self.dismiss(animated: true, completion: nil)

    In the first case , be sure that you embedded your ViewController to a navigationController in your storyboard

    0 讨论(0)
  • 2020-12-04 05:54

    For questions regarding how to embed your viewController to a navigationController in the storyboard:

    1. Open your storyboard where your different viewController are located
    2. Tap the viewController you would like your navigation controller to start from
    3. On the top of Xcode, tap "Editor"
    4. -> Tap embed in
    5. -> Tap "Navigation Controller
    0 讨论(0)
提交回复
热议问题