• UITabBarController使用总结


    刚看了几天教程就开始跟着开发了,以前也没学过C,太痛苦了~只能看看大神的博客,自己再总结学习一下了。

    1.首先新建一个TabBarViewController继承于UITabBarController。然后什么都不用写,相当于装各个tab页的容器。

    2.给每个视图都写一个类,继承于UIViewController,然后只要在viewDidLoad里写属性就ok。除了下面的WebChatViewController还写了一个ContactViewController。

    #import "WebChatViewController.h"
    
    @implementation WebChatViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor redColor];//视图背景颜色
        
        self.title = @"Chat";//视图控制器的标题
        
        self.tabBarItem.title = @"Web Chat";//标签上显示的名称
        self.tabBarItem.image = [UIImage imageNamed:@"tabbar_mainframe.png"];//标签上显示的图片
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_mainframeHL.png"];//选中后的图片
        self.tabBarItem.badgeValue = @"5";//红泡泡
        
        //下面两行暂时还不太理解。。。
        //注意通过tabBarController或者parentViewController可以得到其俯视图控制器(也就是TabBarViewController)
        NSLog(@"%i",self.tabBarController==self.parentViewController);//对于当前应用二者相等
        
    }
    
    @end

    3.在AppDelegate.m里设置TabBar视图为根视图,修改第一个BOOL型的方法即可。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       
        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        
        //初始化
        TabBarViewController *tabBarController = [[TabBarViewController alloc] init];
        WebChatViewController *webChatController = [[WebChatViewController alloc] init];
        ContactViewController *contactController = [[ContactViewController alloc] init];
        
        //把需要的tab都加进去
        tabBarController.viewControllers = @[webChatController,contactController];
        
        //注意默认情况下UITabBarController在加载子视图时是懒加载的,所以这里调用一次contactController,否则在第一次展示时只有第一个控制器tab图标,contactController的tab图标不会显示
        for (UIViewController *controller in tabBarController.viewControllers){
            UIViewController *view = controller.view;
        }
        
        _window.rootViewController = tabBarController;
        [_window makeKeyAndVisible];
        
        return YES;
    }

    运行结果如下:

    导航:

  • 相关阅读:
    修改带!important的css样式
    解决Eclipse导入项目是提示错误:Some projects cannot be imported because they already exist in the workspace
    HTML5——canvas:使用画布绘制卡片
    vue:更改组件样式
    导入导出大量excel文件
    winfrom控件Treeview绑定数据库中的资料(节点控件)
    Winfrom将excel中的数据导入sqlserver数据库中的方法
    C# 将DataTable表中的数据批量插入到数据库表中的方法
    创建Sql数据表的sql代码
    Winfrom之SplitContainer控件
  • 原文地址:https://www.cnblogs.com/brook1223/p/4932870.html
Copyright © 2020-2023  润新知