Pages

TabBarController 加入 NavigationController


/*
 *   ExampleAppDelegate.h
 */
@interface ExampleAppDelegate : NSObject <UIApplicationdelegate>
{
   UIWindow *window;
   UITabBarController *mainBar;
   UINavigationController *firstNav;
   UINavigationController *secondNav;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *mainBar;
@property (nonatomic, retain) UINavigationController *firstNav;
@property (nonatomic, retain) UINavigationController *secondNav;

@end

/*
 *   ExampleAppDelegate.m
 */
#import "ExampleAppDelegate.h"
#import "FirstView.h"
#import "SecondView.h"

@implementation ExampleAppDelegate

@synthesize window;
@synthesize mainBar;
@synthesize firstNav;
@synthesize secondNav;

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   firstNav = [[UINavigationController alloc] init];
   FirstView *firstView = [[FirstView alloc] init];
   [firstNav pushViewController:firstView animated:YES];
   firstNav.tabBarItem.title = @"First Tab";
   [firstView release];

   secondNav = [[UINavigationController alloc] init];
   SecondView *secondView = [[SecondView alloc] init];
   [secondNav pushViewController:secondView animated:YES];
   secondNav.tabBarItem.title = @"Second Tab";
   [secondView release];

   mainBar = [[UITabBarController alloc] init];
   mainBar.viewControllers = [NSArray arrayWithObjects:
                                             firstView, secondView, nil];

   [self.window addSubview:mainBar.view];
   [self.window makeKeyAndVisible];
   return YES; 
}

- (void)dealloc
{
   [firstView release];
   [secondView release];
   [mainBar release];
   [window release];
   [super dealloc];
}

@end
設定Navigation上的標題
/*
 *   FirstView.m
 */
- (void)viewDidLoad
{
   self.navigationItem.title = @"The First View";
}
/*
 *   SecondView.m
 */
- (void)viewDidLoad
{
   self.navigationItem.title = @"The Second View";
}