ios--UITabBarController的用法

来源:互联网 发布:东方财富股票交易软件 编辑:程序博客网 时间:2024/05/17 22:10

课堂实例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
原创粉丝点击