Saturday 17 July 2010

Quick and easy UINavtigationController

I want to add a little section to the app I'm writing to allow the user to edit various settings for the app. Now these settings are a bit more complex than can be provided by the iPhones built in settings app, and I want the users to modify the settings in app as opposed to have to go to the separate Settings app.

So I decided to roll my own.

I have never used a UINavigationController control before, but it makes sense to use one for this area of my app as the settings involve navigating through various nested views - exactly what UINavigationController is designed for.

I pull out my iPhone book and read up on UINavigationController and run through a few tutorials on the net, it all seems easy enough!

Then I try to integrate this into my app.

And the problems begin.. All the tutorials assume the Navigation Controller will be managing all your views and are created in the Root View controller xib. I don't want this, I want it to be created by my settings view.

After several hours of messing about, I got bored and decided to just create one in code. Amazingly this was so easy!

Create the View Controller that you want to set off. Create a navigation controller with your View Controller, set a few options and then call presentModalViewController.

MainSettingsController *controller = [[MainSettingsController alloc] initWithNibName:@"MainSettingsController" bundle:nil];
controller.options = self.options;

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: controller];
nav.navigationItem.prompt = @"Settings";
controller.title = @"settings";
nav.toolbarHidden = false;
[self presentModalViewController:nav animated:YES];



You're done. No faff at all!

1 comment:

  1. To get rid of the navigation controller again, call

    [self dismissModalViewControllerAnimated:YES];

    ReplyDelete