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);

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM