Nivix
Nivix

Reputation: 181

How to go back from one viewcontroller to other viewcontroller

I have a simple one for you guys. I have created a back button on a UITableviewController, and want to set that back button to my other UIViewController(NewTableViewController).

here i implemement back button

let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(NewTableViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton

and action is

func back(sender: UIBarButtonItem) {

   // perform your custom action 
   //....
   // go back to the previuos view controller 
   _ = navigationController?.navigationController?.self.dismiss(animated: true)

 }

but i am unable to go back

Thanks for the help you guys. Please tell me what I am missing.

Upvotes: 0

Views: 407

Answers (1)

Museer Ahamad Ansari
Museer Ahamad Ansari

Reputation: 5523

Try this for swift 3

If you use model view controller

func back(sender: UIBarButtonItem) {
    self.dismiss(animated: true, completion: {});
}

OR

If you use push

func back(sender: UIBarButtonItem) {
    if let navController = self.navigationController {
       navController.popViewController(animated: true)
    }
}

Upvotes: 2

Related Questions