视图控制器总结

老牛浏览 349评论 0发表于

UIViewController、UINavigationController、UITabBarController

1. UIViewController(模态窗口跳转)视图控制器——present、dismiss ViewController

/*
登陆界面——主界面——配置界面
添加按钮执行方法实现跳转,栈结构
present创建对象,跳转动画.modalTransitionStyle,入栈presentViewController
dismiss出栈dismissViewController
*/
//示例:
//a.入栈
NiuConfigViewController *config = [[NiuConfigViewController alloc]init];
config.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:config animated:YES completion:nil];

//b.出栈
[self dismissViewControllerAnimated:YES completion:nil];

2. UINavigationController 导航控制器——push、pop ViewController

objectivec
/*
程序:
1).创建对象
2).创建导航控制器,将对象设置为导航控制器的根视图控制器
3).将导航控制器设置为程序的根视图控制器
navigationItem:title,titleView,leftBarButtonItems,rightBarButtonItem
toolbarItems
*/
//示例:
//a.入栈
NiuSecondViewController *second = [[NiuSecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];

//b.出栈
[self.navigationController popViewControllerAnimated:YES];

//c.返回到指定页
NSArray *array = self.navigationController.viewControllers;
[self.navigationController popToViewController:array[1] animated:YES];

//d.替换指定页(fifth页面替换second页面)
NiuFifthViewController *fifth = [[NiuFifthViewController alloc]init];
NSArray *array = self.navigationController.viewControllers;
NSMutableArray *mutArray = [NSMutableArray arrayWithArray:array];
[mutArray replaceObjectAtIndex:1 withObject:fifth];
self.navigationController.viewControllers = mutArray;
[self.navigationController popToViewController:mutArray[1] animated:YES];

3. UITabBarController 标签控制器

objectivec
/*
程序:
1).创建标签控制器
2).创建多个视图控制器
3).将多个视图控制器添加到标签控制器中
UITabBarItem
*/
NiuSecondViewController *second = [[NiuSecondViewController alloc]init];
second.title = @"second";
second.tabBarItem.image = [UIImage imageNamed:@"tab_1"];
tabController.viewControllers = @[first,second,third,fourth,fifth,sixth];

4. UITabBarController 自定义标签控制器

objectivec
/*
作用:个性化设置的需要(针对某个具体项目)
程序: 1.新建一个视图控制器,继承自UITabBarController
        2.将原标签控制器的标签栏隐藏
        3.在新建的视图控制器内部创建自定义的标签栏
            3.1:1.创建button  2.创建label 3.通过实现button的功能,完成界面跳转
        4.用自定义的标签控制器替换系统的标签控制器
*/
//示例:
NiuFirstViewController *first = [[NiuFirstViewController alloc]init];
first.title = @"first";
UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:first];
NiuSecondViewController *second = [[NiuSecondViewController alloc]init];
second.title = @"second";
UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:second];
self.viewControllers = @[firstNav,secondNav];
self.selectedIndex = 1;

//界面跳转:
self.selectedIndex = button.tag - 10;
//存值:
[[NSUserDefaults standardUserDefaults] setInteger:self.selectedIndex forKey:@"selected"];
//取值:
int mySelected = [[NSUserDefaults standardUserDefaults]integerForKey:@"selected"];
点赞
收藏
暂无评论,快来发表评论吧~