UITabBarController学习案例之一

包括UITabBarController的基本使用:

1.建立7个 UIVIEWController实例;

2.对标签使用了图片;

3.对MORE里面的EDIT使用了代理;

 

主要步骤:

1.建立一个IOS EMPTY PROJECT;

2.command+n,新建7个继承自uiviewcontroller的实例文件;

3.在appdelegate.m里面import这7个头文件;

4.在didFinishLaunchingWithOptions函数里面,填入如下代码:

    RedViewController   *red = [[RedViewController alloc]init];
    GreenViewController *green = [[GreenViewController  alloc]init];
    BlueViewController  *blue = [[BlueViewController alloc] init];
    BlackViewController *black = [[BlackViewController alloc] init];
    YellowViewController *yellow = [[YellowViewController alloc] init];
    PurpleViewController *purple = [[PurpleViewController alloc] init];
    OrangeViewController *orange = [[OrangeViewController alloc] init];
    

    red.title   = @"微信";
    green.title = @"通讯录";
    blue.title  = @"发现";
    black.title = @"我";
    yellow.title = @"1";
    purple.title = @"2";
    orange.title = @"3";
    
//对前两个使用NAV CONTROLLER UINavigationController *redNav = [[UINavigationController alloc] initWithRootViewController:red]; UINavigationController *greenNav = [[UINavigationController alloc] initWithRootViewController:green]; //使用自定义图标 if (0) { red.tabBarItem.image = [UIImage imageNamed:@"tab_0.png"]; green.tabBarItem.image = [UIImage imageNamed:@"tab_1.png"]; blue.tabBarItem.image = [UIImage imageNamed:@"tab_2.png"]; black.tabBarItem.image = [UIImage imageNamed:@"tab_3.png"]; } //使用系统自定义图标 else { /* UITabBarSystemItemMore, UITabBarSystemItemFavorites, UITabBarSystemItemFeatured, UITabBarSystemItemTopRated, UITabBarSystemItemRecents, UITabBarSystemItemContacts, UITabBarSystemItemHistory, UITabBarSystemItemBookmarks, UITabBarSystemItemSearch, UITabBarSystemItemDownloads, UITabBarSystemItemMostRecent, UITabBarSystemItemMostViewed, */

     //设定每个标签的徽标,模拟 "当前有多少信息未读" 场景 red.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1]; red.tabBarItem.badgeValue = @"2"; green.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2]; green.tabBarItem.badgeValue = @"4"; blue.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:3]; blue.tabBarItem.badgeValue = @"100"; black.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:4]; black.tabBarItem.badgeValue = @"14"; }
//字面值进行NSARRAY创建,默认是AUTORELEASE模式,不需要显式 release NSArray *vcArray = @[redNav, greenNav, blueNav, blackNav, yellow, purple, orange];
//使用MVC内存管理 [redNav release]; [blueNav release]; [greenNav release]; [blackNav release]; [red release]; [green release]; [blue release]; [black release]; [yellow release]; [purple release]; [orange release]; //创建标签控制器 UITabBarController *tabController = [[UITabBarController alloc] init]; tabController.viewControllers = vcArray; //加入标签列表 tabController.delegate = self; //设置代理为本APP tabController.selectedIndex = 0; //默认初始时被选择的标签 //输出一个标签栏的尺寸 NSLog(@"height %f", tabController.tabBar.frame.size.height); self.window.rootViewController = tabController; [tabController release];

  接着,因为标签多于5个,会显式MORE标签,里面会有EDIT按钮,以下代码是实现UITabBarControllerDelegate代理协议的:

- (void) tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
    NSLog(@"当点击MORE-EDIT时调用,将要开始时调用");
    [viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop){
        NSLog(@"VC title = %@", obj.title);
    }];
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    NSLog(@"即将结束编辑");
    if (changed) //如果为真,就表示标签位置发生了变化
    {
        UIViewController *obj;
        
        obj = (UIViewController *)[viewControllers objectAtIndex:3];
        
     //如果第四个标签被换成了第五个标签,给一个提示 if([obj.title isEqualToString:@"1"]) { UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"View Changed" message:@"Tab view has changed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease]; [alert show];
       [alert release];
     } } } - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed { NSLog(@"结束编辑"); [viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop){ NSLog(@"VC title = %@", obj.title); }]; }

  

posted on 2014-09-04 19:24  大漠石头  阅读(86)  评论(0)    收藏  举报