IOS: return to the previous view

I have two opinions similar to this on the Story Board:

[View1] → [Navigation Controller] → [View2]

To go from View1 (has a tabular view) to View 2, I do this by clicking a row in View1:

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

It works.

Now I have a button on View 2, which when pressed should return me to the previous view ie View 1.

I tried this:

  navigationController!.popViewControllerAnimated(true) 

But that does not work. How to return to the first view?

+7
ios swift viewcontroller
source share
3 answers

First add a navigation controller to your first view1 through the storyboard. (Editor-> Built-in navigation controller) If there is no navigation.

OR

 self.dismissViewControllerAnimated(true, completion: nil); 

Call this function and try popviewcontroller instead of navigation

+30
source share

Use this code to represent the first view in the action of your button:

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

And do not forget to specify Id for your first view.

Select your first view in storyBoard, and then click on the Identity Inspector, and you can assign a StoryBoard ID as shown below.

enter image description here

+5
source share

self.dismiss (animated: true, completion: zero); it will work in fast 3

0
source share

All Articles