zoukankan      html  css  js  c++  java
  • iOS基础之UITabBarController(标签视图控制器)

      UITabBarController是可以帮我们添加、管理许多的标签项,使我们的程序包含不同的操作模式,由于管理UITabBar可能比较麻烦,系统帮我们对其进行了封装,产生了简单好用的UITabBarController--标签视图控制器。

      代码演示:

      

    #import "AppDelegate.h"
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    #import "ThirdViewController.h"
    #import "FourthViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    -(void)dealloc{
        [_window release];
        [super dealloc];
    }
    - (void)change{
        [[UITabBar appearance]setTintColor:[UIColor greenColor]];
    }
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        firstVC.tabBarItem.title = @"帅哥";//设置显示的文本
    //    firstVC.tabBarItem.badgeValue = @"爱好";//设置显示角标
        [firstVC.button addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
        firstVC.tabBarItem.image = [[UIImage imageNamed:@"iconfont-cool"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];  //设置图标
     
        /*
         第一个参数为标签显示的文本
         第二个参数正常状态下显示的图片
         第三个参数选中状态下显示的图片
         注意,图片想要正常显示,必须是png类型
         */
        
        
        SecondViewController *secondVC = [[SecondViewController alloc]init];
        UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"何二" image:[UIImage imageNamed:@"iconfont-douyixia"] selectedImage:[UIImage imageNamed:@"iconfont-xu"]];
    //    创建好标签之后指定为某一个视图控制器的标签
        secondVC.tabBarItem = item;
        [item release];
        
        //系统自带图标
        ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
        UITabBarItem *systemItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:101];
        thirdVC.tabBarItem = systemItem;
        [systemItem release];
        
        
        
        /*
         第一个参数显示文本
         第二个参数正常状态下显示的图片
         第三个参数标记值
         */
        
        
        FourthViewController *fourthVC = [[FourthViewController alloc]init];
        UITabBarItem *commomItem = [[UITabBarItem alloc]initWithTitle:@"洪荒" image:[UIImage imageNamed:@"iconfont-zhu"] tag:102];
        fourthVC.tabBarItem = commomItem;
        
        //一键换肤
        [[UITabBar appearance]setTintColor:[UIColor redColor]];
        
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
        
        //创建标签视图控制器
        UITabBarController *tabbarController = [[UITabBarController alloc]init];
        //设置标签视图控制器需要管理的子视图控制器
        tabbarController.viewControllers = @[naVC,secondVC,thirdVC,fourthVC];
    //    tabbarController.selectedIndex = 1;
        
        
        //指定window的根视图控制器为标签视图控制器
        self.window.rootViewController = tabbarController;
        [firstVC release];
        [secondVC release];
        [thirdVC release];
        [fourthVC release];
        [naVC release];
        [tabbarController release];

      总之它可以用标签管理多个页面,非常的方便,很多软件都用到它。

  • 相关阅读:
    各种web页面中的绘图技术对比
    32位和64位操作系统
    mysql新建用户本地无法登录
    ruby libmysqlclient.18.dylib
    jenkins创建git任务连接时遇到的问题
    mybatis 打印日志log4j.properties
    使用shell统计出出现次数排名top10的网址(在博客园中没找到,特转一下)
    ActiveMQ和Tomcat的整合应用(转)
    java 哪些情况下会使对象锁释放
    Web容器与Servlet
  • 原文地址:https://www.cnblogs.com/16-jkd/p/5205761.html
Copyright © 2011-2022 走看看