Skip to content

Pushing View Controller onto a Navigation Controller – iPhone SDK

April 12, 2010

I got a request to post up a tutorial about navigating views within the iPhone SDK. First off, make sure that you have a navigation controller set up or you use a “Navigation-based Application” within XCode.

When using a table, there is a UITableViewDelegate mathod called didSelectRowAtIndexPath which is used to handle the selecting of a row within a table view, commonly pushing on to a ‘detail’ view controller. Otherwise, for example, you may use the IBAction of a button to push on to the next view controller, but the means of handling these selection events is up to you.

Once you have the proper method of handling the selection event set up, you need to #import your ‘detail’ view controller, or the view controller that will be next in navigation. Then, there is some fairly elementary allocation, and property setting that needs to be done:

MyViewController *viewController = [[MyViewController alloc] initWithNibName:@”MyNibName” bundle:nil]; —standard allocation/initialization of an objective-c class. If your view controller does not use a nib, you do not need to pass the “initWithNibName” or “bundle” options to it, you may just use the plain “init” method.

viewController.myStringProperty = [NSString stringWithString:@”Hello”]; —this is completely optional, but chances are that your detail view controller needs some passed data for it to do anything useful, yes? This is just like setting any property to any other objective-c class; you need to make sure that the property being set has the @property declaration to it in the .h file.

[self.navigationController pushViewController:viewController animated:YES]; —You are not required to use “animated” as YES, that is if you do not want your application’s view transitions to look like crap. Everything else here is pretty self-explanatory.

[viewController release]; —simply releasing our allocated variable to avoid any memory leak.

So, that is it, it really is not very hard to push viewControllers onto the navigationController in the iPhone/iPad SDK.

Thanks for reading my blog!

For more brief programming tips and news, follow me on twitter @connor20:


From → regular

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: