zoukankan      html  css  js  c++  java
  • UITabBarController 笔记(二) ViewController中加UITabBarController

    新建一个简单视图iOS工程,在ViewController的viewDidLoad中代码如下

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        tabBarCtrl = [[UITabBarController alloc] init];//要加入的TabBarController
        tabBarCtrl.delegate = self;
        //
        firstViewCtl = [[UIViewController alloc] init];
        firstViewCtl.title = @"first view controller";
        firstViewCtl.view.backgroundColor = [UIColor blueColor];
        firstViewCtl.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"rename first" image: nil tag:1];//自定义tabBarItem,可以贴tabBarItem背景图片,如果不写,tabBarItem的标签将显示为firstViewCtl.title内容
    secondViewCtrl = [[UIViewController alloc] init]; secondViewCtrl.title = @"second view controller"; secondViewCtrl.view.backgroundColor = [UIColor redColor]; 
    secondViewCtrl.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",8]; //tabBarItem红色小圈中显示数字
    secondViewCtrl.tabBarItem.tag = 2; //加标示
    NSArray *arryViewContrller = [NSArray arrayWithObjects:firstViewCtl, secondViewCtrl,nil]; tabBarCtrl.viewControllers = arryViewContrller; tabBarCtrl.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    [self.view addSubview:tabBarCtrl.view]; }

    添加委托,ViewController.h中

     
    @interface ViewController : UIViewController<UITabBarDelegate>
    {
        UITabBarController *tabBarCtrl;
        UIViewController *firstViewCtl;
        UIViewController *secondViewCtrl;
    }
     

    tabBarItem点击响应

     
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
        
        NSString *strTitle = viewController.title;
        NSLog(@"Title:%@ tag:%d", strTitle  , viewController.tabBarItem.tag);

         if(1 == viewController.tabBarItem.tag)

       {

            viewController.tabBarItem.badgeValue = [NSStringstringWithFormat:@"%d", rand()%100];

        }

    }
     

     小结:

    UITabBarController 继承自UIViewController,所以嘛。。。。。不多说了,你懂的!!
  • 相关阅读:
    开源跨平台数据格式化框架概览
    (12) MVC5 EF6 Bootstrap3
    前端构建利器Grunt—Bower
    深入理解JavaScript系列(33):设计模式之策略模式(转)
    为什么MVC不是一种设计模式(转)
    java Double保留小数点位数
    网线直接连接电脑可以上网,但通过无线路由器时却上不了网(转)
    How to install PL/SQL developer on linux (转)
    自己动手写CPU之第八阶段(4)——转移指令实现过程2
    Eclipse中SVN的安装步骤(两种)和用法
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5009843.html
Copyright © 2011-2022 走看看