UITabBarController的用法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]] autorelease];

    FirstViewController *firstController=[[FirstViewController allocinit];

    //设置标题

    firstController.title=@"First";

    //设置tabBarItem的样式(这种方式是按照系统定义的方式)

    firstController.tabBarItem=[[UITabBarItem allocinitWithTabBarSystemItem:UITabBarSystemItemMoretag:101];

    //也可以用自定义方式方式如下:

    /*

    firstController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"img.png"] tag:101];*/

    

    SecondViewController *secondController=[[SecondViewController allocinit];

    secondController.title=@"Second";

    secondController.tabBarItem=[[UITabBarItem allocinitWithTabBarSystemItem:UITabBarSystemItemHistorytag:102];

    ThirdViewController *thirdController=[[ThirdViewController allocinit];

    thirdController.title=@"Third";

    thirdController.tabBarItem=[[UITabBarItem allocinitWithTabBarSystemItem:UITabBarSystemItemFavoritestag:103];

    

    UINavigationController *nav1=[[UINavigationController allocinitWithRootViewController:firstController];

    [firstController release];

    UINavigationController *nav2=[[UINavigationController allocinitWithRootViewController:secondController];

    [secondController release];

    UINavigationController *nav3=[[UINavigationController allocinitWithRootViewController:thirdController];

    [thirdController release];

    NSArray *controllersArray=[[NSArrayallocinitWithObjects:nav1,nav2,nav3, nil];

    [nav1 release];

    [nav2 release];

    [nav3 release];

    

    UITabBarController *tabController=[[UITabBarController allocinit];

    tabController.viewControllers=controllersArray;

    tabController.selectedIndex=0;

    tabController.delegate=self;//AppDelegate.h文件内实现UITabBarControllerDelegate协议

    self.tabBarController=tabController;

    [tabController release];

    [controllersArray release];

    

    [self.window addSubview:tabController.view];

 

    [self.window makeKeyAndVisible];

    returnYES;

}

 

//当点击tabBarItem时触发该操作  UITabBarControllerDelegate中的一个方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

posted on 2012-03-25 17:16  iYiming  阅读(4004)  评论(0编辑  收藏  举报

导航