Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Go back to previous view

I have two views like this in Story Board:

[View1] -> [Navigation Controller] -> [View2]

To go from View1(has tableview) to View 2, i do this on click of a row on View1:

self.performSegueWithIdentifier("showmyview2", sender:self)

This works.

Now i have a button on View 2, which when clicked should take me back to previous view i.e View 1.

I tried this:

 navigationController!.popViewControllerAnimated(true)

But this does not work. How do i go back to first view?

like image 372
Jasper Avatar asked May 15 '15 10:05

Jasper


People also ask

How do I pop a view controller in Swift?

You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point (the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.


2 Answers

First Add a navigation controller to your First view1 through storyboard.(Editor->Embed Navigation controller), If navigation is not present.

OR

self.dismissViewControllerAnimated(true, completion: nil);

Call this function and try instead of navigation popviewcontroller

like image 54
Mukesh Avatar answered Nov 15 '22 07:11

Mukesh


Use this code to present first view in your button action:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("FirstView") as! TableViewController
self.presentViewController(vc, animated: true, completion: nil)

And don't forget to give Id to your First view.

Select your firstView into storyBoard and then click on Identity Inspector and you can assign StoryBoard ID like show into below Image.

enter image description here

like image 42
Dharmesh Kheni Avatar answered Nov 15 '22 07:11

Dharmesh Kheni