在《视图控制器》中通过button简单实现了视图的切换。此文通过UITabBarController和UINavigationController简单介绍,实现视图的切换
(一)UITabBarController简介
UITabBarController功能为应用屏幕下方的功能键,如打电话应用下方的最近通话/通讯录等按钮。又或者为微信下方的通讯录/发现/我等按钮。
(二)新建两个UIViewController
如下图所示,并在相应的viewDidLoad中设置不同的背景颜色以区分
(三)新建一个LLTabBarController,并继承在UITabBarController
在.m文件中添加新的init方法,在初始化中,将(二)中新建的视图赋值给LLTabBarController,如下所示
-(id) init { if(self==[super init]) { TabBarView1Controller *view1=[[TabBarView1Controller alloc] init]; //设置显示样式 view1.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0]; TabBarView2Controller *view2=[[TabBarView2Controller alloc] init]; view2.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0]; NSArray *views=[[NSArray alloc] init]; views=[views arrayByAddingObject:view1]; views=[views arrayByAddingObject:view2]; [self setViewControllers:views]; } return self; }
(四)在AppDelegate.m中设置跟视图控制器
UITabBarViewController也继承在UIViewController,可以设置其为根控制器,也可以设置其他的UIViewController为根视图,并把UITabBarViewController,附在根视图上(addSubview)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch LLTabBarController *tabBarController=[[LLTabBarController alloc] init]; self.window.rootViewController=tabBarController; tabBarController.selectedIndex=1; //UIViewController *selectedController=tabBarController.selectedViewController; tabBarController.delegate=self; return YES; } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"Did select view controller"); }
至此,可以通过底部按钮进行相应切换了。在项目中,UITabBarController跟UINavigationContorller经常会搭配使用,比如UITabBarController每个tab的rootViewController就是一个UINavigationController
//---------------------------------分割线------------------------------------//
UINavigationController通过出入栈方式实现视图控制以及相关按钮实现
(一)新建LLNavigationController并继承自UINavigationController
(二)新建View1Controller(作为被控制视图)
(三)在AppDelegate.m中添加如下代码,并把LLNavigationController设置为根控制视图
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch LLNavigationController *navigationController=[[LLNavigationController alloc] init]; self.window.rootViewController=navigationController; navigationController.delegate=self; //定义初始controller View1Controller *mainViewController=[[View1Controller alloc] init];
//设置初始视图
[navigationController pushViewController:mainViewController animated:YES]; return YES; }
(四)在View1Controller中完成LLNavigationController相关设置
也可以在其他地方设置LLNavigationController参数以及方法,如AppDelegate中。在View1Controller中设置时为了说明通过navigationController变量可以获取到LLNavigationController,并进行视图的切换
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //导航栏设置,如果没有在LLNavigationController中进行入栈(pushViewController)操作,则无效 self.navigationItem.title=@"weichat"; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBtnEvent)]; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editBtnEvent)]; } -(void) addBtnEvent { NSLog(@"Add clicked"); } -(void) editBtnEvent { NSLog(@"Edit clicked"); //可以在主controller中获取到navigationController,bing设置显示其他controller //[self.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>] }
至此可实现LLNavigationController简单功能
原文地址:https://www.cnblogs.com/llstart-new0201/p/9690648.html