I want to implement TabBarController and add Navigation Controller View but in vain
Iknow how to add a TabBar with two TabBar buttons on it by Interface Builder. And I link each button to a Navigation Controller.
Now I want to learn how to do everything programmatically. Now I can see a TabBar in simulator but with no buttons on it. Can someone please help me with this. Thanks!
Here's the TabBarAppDelegate.h.
#import <UIKit/UIKit.h>
@interface TabBarAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
IBOutlet UINavigationController *navigationController1;
IBOutlet UINavigationController *navigationController2;
}
@property (nonatomic, retain) 开发者_开发知识库IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController1;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController2;
@end
Here's TabBarAppDelegate.m.
#import "TabBarAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation TabBarAppDelegate
@synthesize window=window;
@synthesize tabBarController;
@synthesize navigationController1;
@synthesize navigationController2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = [[UITabBarController alloc]init];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
firstViewController.title = @"First";
[self.navigationController1 pushViewController:firstViewController animated:NO];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.title = @"NavCal";
[self.navigationController2 pushViewController:secondViewController animated:NO];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[firstViewController release];
[secondViewController release];
return YES;
}
- (void)dealloc
{
[tabBarController release];
[window release];
[super dealloc];
}
It looks like you are not initializing the navigation view controllers. See if this would work:
TabBarAppDelegate.h
- Remove the properties navigationController1 and navigationController2
TabBarAppDelegate.m
Replace
[self.navigationController1 pushViewController:firstViewController animated:NO];
with
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstViewController];
Replace
[self.navigationController2 pushViewController:secondViewController animated:NO];
with
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
- Release navigationController1 and navigationController2 after adding them to the tabBarController
精彩评论