zoukankan      html  css  js  c++  java
  • tab bar controller

    下面记一下怎样通过代码的方式为选项卡添加视图。

    1、创建一个基于Empty Application的项目

    2、创建两个新类,基类选择UIViewController,勾选With XIB for user interface分别命名为"OneController'和"TwoController",

    3、分别更改OneController.xib和TwoController.xib文件的view背景颜色,便于区分

    4、在AppDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数做如下修改(记得导入OneController和TwoController的头文件)

    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
    4.   
    5.     //将tabBar(选项卡)添加进来   
    6.     UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];  
    7.       
    8.     //为选项卡添加子控制器   
    9.     OneController *one = [[[OneController alloc] init] autorelease];  
    10.     [tabBarController addChildViewController:one];  
    11.       
    12.     TwoController *two = [[[TwoController alloc] init] autorelease];  
    13.     [tabBarController addChildViewController:two];  
    14.       
    15.       
    16.       
    17.     self.window.rootViewController = tabBarController;  
    18.       
    19.     [self.window makeKeyAndVisible];  
    20.     return YES;  
    21. }  
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
        //将tabBar(选项卡)添加进来
        UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
        
        //为选项卡添加子控制器
        OneController *one = [[[OneController alloc] init] autorelease];
        [tabBarController addChildViewController:one];
        
        TwoController *two = [[[TwoController alloc] init] autorelease];
        [tabBarController addChildViewController:two];
        
        
        
        self.window.rootViewController = tabBarController;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    运行效果如下:

    现在创建好的选项卡下面是没有图标和文字的,,,现在我们通过代码给它们添加一些图标和文字,注意,,选项卡的图标和文字是子控制器决定的而不是tab Bar Controller,,这点要记住。

    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
    4.   
    5.     //将tabBar(选项卡)添加进来   
    6.     UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];  
    7.       
    8.     //为选项卡添加子控制器   
    9.     OneController *one = [[[OneController alloc] init] autorelease];  
    10.     one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标   
    11.     [tabBarController addChildViewController:one];  
    12.       
    13.     TwoController *two = [[[TwoController alloc] init] autorelease];  
    14.     //添加一个自定义的图标和文字   
    15.     two.tabBarItem.title = @"two";  
    16.     two.tabBarItem.image = [UIImage imageNamed:@"success.png"];  
    17.     [tabBarController addChildViewController:two];  
    18.       
    19.       
    20.       
    21.     self.window.rootViewController = tabBarController;  
    22.       
    23.     [self.window makeKeyAndVisible];  
    24.     return YES;  
    25. }  
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
        //将tabBar(选项卡)添加进来
        UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
        
        //为选项卡添加子控制器
        OneController *one = [[[OneController alloc] init] autorelease];
        one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标
        [tabBarController addChildViewController:one];
        
        TwoController *two = [[[TwoController alloc] init] autorelease];
        //添加一个自定义的图标和文字
        two.tabBarItem.title = @"two";
        two.tabBarItem.image = [UIImage imageNamed:@"success.png"];
        [tabBarController addChildViewController:two];
        
        
        
        self.window.rootViewController = tabBarController;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    以上代码中,我在第一个Controll View 中添加了一个系统自带的下载图标,,在第二个Controller View中添加了一个自定义的图标(先将图标导入到项目中)和文字。

    运行效果如下:

    以上的所有代码我都是在AppDelegate.m文件中得

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数中实现的,,,这仅仅只是为了操作方便才这样写的,,大多数情况下是写在该tab Bar Controller 的实现文件中的,如这里是在的MyTabController.m文件中的- (id)init{ } 函数中实现的。

  • 相关阅读:
    本地复现Zabbix v2.2.x, 3.0.0-3.0.3 jsrpc 参数 profileIdx2 SQL 注入漏洞
    本地搭建复现st2-045漏洞
    Ubuntu安装Vulapps漏洞靶场
    如何在腾讯云Ubuntu服务器安装kali下的神器
    nginx 跳转配置
    Chocolatey 的安装
    MySQL 5.1 主从同步配置
    针对Windows Server 2008 Web 服务 IIS+php 配置的一些心得
    解决IIS7+php的组合上传限制30M的问题
    ssh 文件权限影响登录
  • 原文地址:https://www.cnblogs.com/yulang314/p/3568333.html
Copyright © 2011-2022 走看看