ios--UITabBarController的用法

课堂实例2

        完善实例1

(1)自定义一个UITabBarController实例,在此类中添加若干个视图控制器

(2)添加代理方法,查看当前的那一个视图控制器

   (3)  是这自定义一个TabBarItem图片

代码如下:

AppDelegate.m中的代码

  /*思路:
     *1.创建若干个子视图控制器(它们是并列的关系)
     * 1.1创建UITabBarItem实例,赋值给相应的子视图控制器(2中方法)
     *2.创建一个数组,将已创建的子视图控制器,添加到数组中
     *3.创建UITabBarController实例
     *4.tabBarController.viewControllers = viewControllers;
     *5.添加到window的rootViewController中(显示出来)
     */
    
    homeViewController *vc1 = [[homeViewController  alloc]init];
    UITabBarItem *homeItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    vc1.tabBarItem = homeItem;
    //vc1.title = @"首页";
    //vc1.view.backgroundColor = [UIColor redColor];
    
    MesageViewController *vc2 = [[MesageViewController alloc]init];
    UITabBarItem *msmItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
    vc2.tabBarItem = msmItem;

    //vc2.title = @"新闻";
   // vc2.view.backgroundColor = [UIColor blueColor];
    
    SearchViewController *vc3 = [[SearchViewController alloc]init];
    UITabBarItem *sacItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:3];
    vc3.tabBarItem = sacItem;

    //vc3.title = @"历史";
    //vc3.view.backgroundColor = [UIColor grayColor];
    
    settingViewController *vc4 = [[settingViewController alloc]init];
    UITabBarItem *setItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMore tag:4];
    vc4.tabBarItem = setItem;

    //vc4.title = @"搜索";
    //vc4.view.backgroundColor = [UIColor yellowColor];
    
    
    
    NSArray *viewControllers = @[vc1,vc2,vc3,vc4];//把他们放在数组中
    UITabBarController *tabBarController = [[UITabBarController alloc]init];     //初始化
    [tabBarController setViewControllers:viewControllers animated:YES];
    self.window.rootViewController = tabBarController;
    
homeViewController.m的代码:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"首页";
    }
    return self;
}

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    view.backgroundColor = [UIColor redColor];
    self.view = view;
}
MessageViewController.m中的代码:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新闻";
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    view.backgroundColor = [UIColor greenColor];
    self.view = view;
}
SearchViewController.m中的代码:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"搜索";
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    view.backgroundColor = [UIColor blueColor];
    self.view = view;
}
SettingViewController.m中的代码:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"设置";
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    view.backgroundColor = [UIColor yellowColor];
    self.view = view;
}
模拟器显示如图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值