Andrew
Andrew

Reputation: 16051

Switch between views with UINavigationBar

I've went back to hello world tutorials trying to do this. I can't seem to figure this out for some reason, and yet it should be so simple.

I want to have a UINavigationBar with a button on the right side. When the user presses this button, it takes them to a second view through the slide-to-the-side animation, and on this new view the navigationbar shows a back button to the previous view.

How can i get this to happen? I can't for the life of me figure it out. Is there a tutorial somewhere which goes over it? I can't find one.

Upvotes: 1

Views: 795

Answers (2)

Bourne
Bourne

Reputation: 10312

On the button action (the selector) use the following message on self.navigationController

 pushViewController:secondViewController animated:YES

EDIT: Create the UINavigationController as follows:

UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

And then set the right button as required suggested by Sorin in his answer. And use pushViewController:animated: message on self.navigationController as I have already posted in my original answer. Hope it helps. rootViewController is the view controller you want to have pushed as the first view on your navigation stack.

Upvotes: 0

Sorin Antohi
Sorin Antohi

Reputation: 6135

You should create an UINavigationController with an UIViewController as a root. in the UIViewController you should setup the bar right button. you should have something similar to this:

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
                        initWithTitle: @"Next"
                        style:UIBarButtonItemStyleDone 
                        target:self 
                        action:@selector(nextPage:)] 
                        autorelease];

when you touch the button the method nextPage: will be called and will execute the push of the new view.

-(void)nextPage:(id)sender
{
   UIViewController *secondViewController = [[UIViewController alloc] init];
   [self.navigationController pushViewController:secondViewController animated:YES];
   [secondViewController release];
}

here is a tutorial in two parts for using an UINavigationController and here is the official documentation for the UINavigationController(really useful).

Upvotes: 3

Related Questions