I am following Stanford's iPhone Development course in iTunes U and I'm having problem with one of their assignments (Paparazzi, if anyone is familiar).
What I'm trying to do is basically to create this view as the first 'screen' upon applicat开发者_开发百科ion launch:
http://cl.ly/1USw/content
And this is the code that I have in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// PersonListViewController is the 'content' of the screen (image, name, and button)
PersonListViewController* personList = [[PersonListViewController alloc] initWithNibName:@"PersonListViewController" bundle:[NSBundle mainBundle]];
navController = [[UINavigationController alloc] initWithRootViewController:personList];
[personList release];
UITabBarController* tabBarController = [[UITabBarController alloc] init];
[window addSubview:[tabBarController view]];
[window addSubview:[navController view]];
[window makeKeyAndVisible];
return YES;
}
But this code doesn't seem to show the tabBar on the bottom, only the navigation bar and the view in the middle (image, name, and the button).
Can someone explain to me what I did wrong and how to fix it?
What you typically do is nest your main ViewController (in this case, PersonListViewController
) inside a UINavigationController
(like you have done), but then you set the tabBarController.viewControllers
property equal to an array of view controllers (one for each tab).
In your case, that would look like
tabBarController.viewControllers = [NSArray arrayWithObject:navController];
Then add only the tabBarController's view to the window
[window addSubview:[tabBarController view];
This will give you your list, inside a nav controller, inside a tab controller (and the tab controller will only have one tab, for now).
UIWindow doesn't cope well if you try to put views from two view controllers into it: only the last one will get the appropriate callbacks, and they won't know about each other. (I have done this intentionally, when I wanted a constant animated background behind a transparent set of views, but it's tricky.) In ios4 this is more explicit: UIWindow has grown a "rootViewController" property which holds the VC for the window.
What you want here is a tab bar controller containing navigation controllers, one per tab, each containing one of your custom VCs.
sort of like: (this is off the cuff and untested, so watch for errors)
PersonListViewController* plvc = [[[PersonListViewController alloc]
initWithNibName:@"PersonListViewController"
bundle:nil]
autorelease];
UINavigationController *uinc = [[[UINavigationController alloc]
initWithRootViewController:plvc]
autorelease];
// ... make more VCs for any other tab pages here
UITabBarController* tbc = [[[UITabBarController alloc] init] autorelease];
[tbc setViewControllers:[NSArray arrayWithObjects: uinc, nil]]; // *1
/* iOS 4 and later, preferred: */
[window setRootViewController:tbc];
/* or, alternatively, in iOS 3:
[window addSubview:[tbc view]];
[self setMyMainTabsController:tbc]; // must keep an owning reference or it'll get released
*/
[window makeKeyAndVisible];
at *1, add any VCs for other tabs to the list.
Putting nav controllers inside a tab controller is perfectly legit and Apple-endorsed. Putting tab controllers inside nav controllers isn't, though it may work in limited cases.
精彩评论