分栏视图控制器

UITabBarController

【分栏视图控制器】

UITabBarController是分栏视图控制器,是一个容器视图控制器,

用于切换并行关系的视图,而非具有层次关系的视图间的切换。

1、认识UITabBarController

a、创建分栏视图控制器

UITabBarController *tabBarController = [[UITabBarController alloc]init];

b、所管理的视图控制器

tabBarController.viewControllers = 存放视图控制器的数组

c、设置整个条的背景色

tabBarController.tabBar.barTintColor = [UIColor yellowColor];

d、设置选中状态的颜色

tabBarController.tabBar.tintColor = [UIColor blueColor];

 //设置默认选中tabbaritem

    tabBarController.selectedIndex = 2;

e、设置代理及代理方法

item的类型

UITabBarSystemItem sysls[] = {

            UITabBarSystemItemFavorites,

            UITabBarSystemItemFeatured,

            UITabBarSystemItemTopRated,

            UITabBarSystemItemRecents,

            UITabBarSystemItemContacts

    };

创建一个视图控制器,设置视图控制器item的类型

NSString *className = [NSString stringWithFormat:@"ViewController%d",i];

        Class cls = NSClassFromString(className);

        BaseViewController *vc = [[cls alloc]init];

        UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:sysls[i] tag:100 + i];

        vc.tabBarItem = item;

        [resultArray addObject:vc];

2、定制UITabBarController

 (1)创建tabbarController的子视图控制器

用数组保存要创建的每个分栏的名称,保存每个分栏的图片名称 

比如NSArray *titles = @[@"微信",@"通讯录",@"发现",@"我"];”

NSArray *imageNames = @[@"tabbar_mainframe",@"tabbar_contacts",@"tabbar_discover",@"tabbar_me"];

用“for”循环创建每个视图控制器 

比如

for (int i = 0; i < 4; i++) {

        NSString *className = [NSString stringWithFormat:@"ViewController%d",i];

        Class cls = NSClassFromString(className);

        BaseViewController *vc = [[cls alloc]init];

        NSString *title = titles[i];

        NSString *imageName = imageNames[i];

        NSString *imageHLName = [imageName stringByAppendingString:@"HL"];    

        //保持原始模式 ,设置图片的渲染模式为不渲染

        UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

        UIImage *imageHL = [[UIImage imageNamed:imageHLName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];   

        //标题 正常状态的图片和选中状态的图片

        UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:title image:image selectedImage:imageHL];

        vc.tabBarItem =item;

        //设置导航控制器的标题

        vc.title = titles[i];

        //title的颜色和大小

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,[UIColor greenColor],NSForegroundColorAttributeName, nil];

       [item setTitleTextAttributes:dict forState:UIControlStateNormal];

        //创建一个导航视图控制器

        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

将nav存放到一个数组中

[resultArray addObject:nav]; 

(2)在viewDidLoad中直接调用

  //设置tabbarcontroller的子视图控制器

    self.viewControllers = [self createViewControllers]; 

3、沙盒

//sandBox沙盒

//安全机制,每个应用程序都有单独的空间,当需要访问其他应用时,需要请求系统允许

//提供了一种数据持久化机制

 //获得NSUserDefaults的单例

    NSUserDefaults *userDefaults = [NSUserDefaults  standardUserDefaults];

    //存一个整形变量

    [userDefaults setInteger:10 forKey:@"unReadMessageNumCount"];

    //同步保存

    [userDefaults synchronize];

    //读取数据

    NSInteger num = [userDefaults integerForKey:@"unReadMessageNumCount"];

//知道用户是否登录

    //登录成功

    [userDefaults setBool:YES forKey:@"isLogin"];

    //每次启动应用检测isLogin的值

    BOOL isLogin = [userDefaults boolForKey:@"isLogin"];

    if (isLogin == YES) {

        NSLog(@"已经登录,进入程序的主界面");

    }

    else{

        NSLog(@"没有登录,进入登录界面");

    }

    NSLog(@"程序的主目录%@",NSHomeDirectory());

原文地址:https://www.cnblogs.com/jiang-xiao-yan/p/5952499.html