views:

1238

answers:

3

I have been struggling a little with my first real iPhone application and wanted to get some advice on how this should be structured. I am looking for some best practices in terms of creating UI components and linking them together to create the application flow (create views/controllers programmaticly vs. with Interface Builder, ect...).

Overview:
I need to show a "Login" view on application start up.
--Show "Signup" view if they click the sign up button.

Once logged in... I have a TabBarController loading 4 views. These 4 views will have to load sub-views (master-detail like).

My question is:
1) What is the best way to piece this navigation structure together? Create each view as a .xib with a corresponding ViewController? How are these glued together?

2) How should I handle the Login/Sign up navigation, no TabBar should be shown on start, but will need it after authenticating the user.

Bonus Point) Are there documented best practices for this kind of stuff? I have been hacking together some workable code, but I got very lost and want to start over doing it the correct way.

I know this may be a little confusing, all and any help is much appreciated.

EDIT: For the Login view on top of the tab bar I used this, pretty simple.

LoginViewController *loginViewController = [[LoginViewController alloc] init];
[loginViewController initWithNibName:@"Login" bundle:nil]; 
[self.tabBarController presentModalViewController:loginViewController animated:YES];
A: 

In my opinion, only Cocoa programming examples and very simple applications are suitable for a single nib file. Otherwise you should spread your interface components across multiple nibs. This means each nib is smaller and when loaded into memory will only load those components as necessary. This will improve performance in your application and can help logically organize your program and make it easier to debug when issues arise.

In my tabbar apps I use MainWindow.xib to contain the main window and tabbar but I break up each tab into a seperate nib for the reasons above.

Apple offers the following guidelines:

When creating your nib files, try to keep the following guidelines in mind:

Design your nib files with lazy loading in mind. Plan on loading nib files that contain only those objects you need right away.

In the main nib file for a Mac OS X application, consider storing only the application menu bar and an optional application delegate object in the nib file. Avoid including any windows or user-interface elements that will not be used until after the application has launched. Instead, place those resources in separate nib files and load them as needed after launch.

Store repeated user-interface components (such as document windows) in separate nib files.

For a window or menu that is used only occasionally, store it in a separate nib file. By storing it in a separate nib file, you load the resource into memory only if it is actually used.

For more information you can visit:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4

radesix
+1  A: 

1) What is the best way to piece this navigation structure together? Create each view as a .xib with a corresponding ViewController? How are these glued together?

Use UINavigationController and push your custom views onto the navigation stack as needed. Check out the example Navigation Controller application via Xcode's New Project option to get a feel for how this works.

2) How should I handle the Login/Sign up navigation, no TabBar should be shown on start, but will need it after authenticating the user.

Set up a view for login (I would use a UITableView with one section containing two rows for username and password, but that's my own preference). Set up a second, separate view for sign-up fields (again, I would use a UITableView for this, to keep the layout clean and consistent).

Perhaps use a view animation to pop up the tab bar after successful authentication.

Bonus Point) Are there documented best practices for this kind of stuff? I have been hacking together some workable code, but I got very lost and want to start over doing it the correct way.

You'll end up rewriting your project several times — which is a good thing. Do check out Apple's sample applications (available from the iPhone ADC site) as these contain several "best practice" ways of using several of the UIKit components. As to putting together a larger application, keep your design as simple as possible and reuse as much of Apple's UI components as possible. You can always customize later.

Alex Reynolds
+1  A: 

When you create a new tab bar based application in XCode, you are pretty much already set up the way you would like - there's a main XIB that loads views for each tab from separate XIB files. You have one XIB per tab. Note that as you change types or add tabs you need to specify the proper view controller type in both the XIB with the tab bar, and in the XIB that you use to create your view!

As for the login view, a common approach is to use the tab bar as above, but in the app delegate applicationDidFinishLaunching method present a modal view controller that shows the login screen. The modal controller hides the tab bar and everything else until they are done, then it can be dismissed.

Kendall Helmstetter Gelner
Can you elaborate on the presentModalViewController call? It looks like I have to call that on a navigation controller, I only have a tabbarcontroller.
JWD
presentModalViewController is a method on UIViewController, not just navigation controllers - you can use a tab bar controller or any other view controller.
Kendall Helmstetter Gelner