UITabBarController 使用


NCAppDelegate.h
#import <UIKit/UIKit.h>

@interface NCAppDelegate : UIResponder <UIApplicationDelegate> {
    UIWindow *window;
	UITabBarController *tabBarController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;

@end



NCAppDelegate.m
@synthesize window;
@synthesize tabBarController;

- (void)dealloc
{
    [window release];
    [tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    tabBarController = [[UITabBarController alloc] init];
    
    HomeViewController *vc1 = [[HomeViewController alloc] init];
    MetionsViewController *vc2 = [[MetionsViewController alloc] init];
    FavoritesViewController *vc3 = [[FavoritesViewController alloc] init];
    ProfileViewController *vc4 = [[ProfileViewController alloc] init];
    
    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2,vc3,vc4, nil];
    
    tabBarController.viewControllers = controllers;
    
    [window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    return YES;
}


HomeViewController.m
@implementation HomeViewController

- (id)init {
    if (self = [super init]) {
        self.title = @"主页";
        UIImage* anImage = [UIImage imageNamed:@"home_default.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:anImage tag:0];
        self.tabBarItem = theItem;
        [theItem release];
    }
    return self;
}

猜你喜欢

转载自zl4393753.iteye.com/blog/1680139