Florian Faust

Win, Mac and everything in between

  • Increase font size
  • Default font size
  • Decrease font size
Home iPhone OS Navigating and Cleaning up properly

Navigating and Cleaning up properly

 

Navigating and Cleaning up properly



After having learned the basics for building guis and storing our data, its time to connect the views to create more complex applications. The UINavigationController is a good starting point for building an application with stacked views.

I added the default UINavigationController in the main NIB file, the Controller will hide the nasty details of managing the views we want to display. If you do not need any special gui behaviour the default Controller should be sufficient. For better access to the Controller you should store a reference to the UINavigationController in your ApplicationDelegate. Now your application has the capabilities to manage multiple views.

If you create a single NIB file for every seperate view of your application you don't need to add a UINavigationController nor a UINavigationBar to this NIB files. The only thing you might add is a UIBarButtonItem if you want to display an additional button in the NavigationBar.

Starting from your main view you now just need to implement an Action, so a new view is pushed onto the view stack of the UINavigationController, causing this new view to be displayed.

 


// Create the view for displaying some model details
DetailViewController* pDetailViewController = [[DetailViewController alloc] initWithNibName:@"ModelDetailView" bundle:nil];
// assign the model to edit to our view
[pDetailViewController setModelData:pModel];
// Push the view onto the stack
ToDoAppDelegate* pDelegate = [[UIApplication sharedApplication] delegate];
[[pDelegate navigationController] pushViewController: pDetailViewController animated:TRUE];
// Clean up
[pDetailViewController release];


Something you should take care of right from the beginning is memory management.

If you are not familiar with the handling of memory allocation/deallocation on the Apple/iPhone platform read the appropriate documentation provided by Apple. Especially on the iPhone it is crucial to take good care of your resources.

The more the complexity in your application grows the more possible memory holes can be digged. So its good practice to handle these things right from the beginning carefully, else you might spend some time with Instruments to find all your leaks.