Tony
Tony

Reputation: 287

Change view controller on press button

I have two View Controllers, I need to have a button in ViewControllerOne that when I press it Show me ViewControllerTwo.

In storyboard I related both views with a "Presenting Segues" - Push modal. And both views have a view controller class.

Upvotes: 3

Views: 6682

Answers (2)

Rob
Rob

Reputation: 438222

If you want to do a push segue, it's even easier. First, if you don't already have a navigation controller, add one by selecting the first view and choose "Embed in" - "Navigation Controller":

push 1

When you do this, you'll have a new navigation controller (which you don't really need to do much with) and the first view will have a navigation bar.

push 2

Now, right-click (or control-click) on your first view's button and drag over to the second view:

push 3

This time, select the "push" segue:

push 4

And you'll know that it worked, because your second view will have a navigation controller

enter image description here

You don't need a button to go back, because the navigation controller will automatically have a "Back" button, so you don't need to add your own.

This is how you achieve a push segue.

Upvotes: 5

Rob
Rob

Reputation: 438222

I'm not sure what you mean by "I related both views with a 'Presenting Segues' - Push modal." Are you using a navigation controller and want a push segue, or do you want to do a modal segue? A "push modal" is a contradiction in terms.

So, let's imagine that you want a modal segue. So, you put a button on the first view, right-click and drag (or control-click and drag) from that button to the second view.

create segue 1

You'll get a pop up asking for type of segue. Select "modal".

create segue 2

And you're done transitioning from 1 to 2. No code necessary.

If you want a button on the second view to take you back to the first view, you do not want a modal segue from the second view back to the first view, but rather you want to dismissViewControllerAnimated. You can do this via a custom segue, or easier, just have a button which calls dismissViewControllerAnimated. Thus, you'd add a button to the second view, and while the editor is in in "assistant" mode (where the associated .h file is showing below the interface builder; see below if you want to know how to show the .h file at the same time as the Interface Builder screen), right-click (or control-click) and drag from the button on the second view down to the second view controller's .h file:

dismiss 1

By the way, if you don't see the .h file there, click on the "assistant" editor button and choose "automatic" for the files to be shown down there, and you should be good:

assistant editor

It will then show you a pop up asking you what you want to do. Select IBAction and give your new method a name:

dismiss 2

Then go to your code for the view controller, and add the dismissViewControllerAnimated code:

enter image description here

All that code says (and in this example, I just called my IBAction dismissTwo) is:

- (IBAction)dismissTwo:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 9

Related Questions