代码方式创建导航
UINavigationController是一个导航控制器,它用来组织有层次关系的视图,在UINavigationController中 子控制器以栈的形式存储,只有在栈顶的控制器能够显示在界面中,一旦一个子控制器出栈则会被销毁。UINavigationController默认也不 会显示任何视图(这个控制器自身的UIView不会显示),它必须有一个根控制器rootViewController,而且这个根控制器不会像其他子控 制器一样被销毁。
下面简单通过几个视图模拟一下微信添加好友的功能,假设有一个导航控制器,它的根控制器为好友列表控制器 KCFriendViewController,通过它可以导航到添加QQ联系人视图KCQQContactViewController,在QQ联系人 视图又可以导航到公共账号视图KCPublicAccountViewController。
1.首先在应用代理启动后初始化一个导航控制器并设置其根控制器为KCFriendViewController
- //
- // AppDelegate.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "KCTabBarViewController.h"
- #import "KCWebChatViewController.h"
- #import "KCContactViewController.h"
- #import "KCFriendViewController.h"
- #import "KCQQContactViewController.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
- //设置全局导航条风格和颜色
- [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
- [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
- KCFriendViewController *friendController=[[KCFriendViewController alloc]init];
- UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:friendController];
- _window.rootViewController=navigationController;
- [_window makeKeyAndVisible];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application {
- // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- @end
2.在好友列表视图控制器中设置导航栏左右按钮,并且设置点击右侧按钮导航到添加QQ联系人视图
- //
- // KCFriendViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCFriendViewController.h"
- #import "KCQQContactViewController.h"
- @interface KCFriendViewController ()
- @end
- @implementation KCFriendViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //每次出栈都会销毁相应的子控制器
- NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers);
- //在子视图中可以通过navigationController属性访问导航控制器,
- //同时对于当前子视图来说其父控制器就是其导航控制器
- NSLog(@"%i",self.navigationController==self.parentViewController);
- //在子视图中(或者根视图)有一个navigationItem用于访问其导航信息
- self.navigationItem.title=@"Friends";//或者直接设置控制器title(例如[self setTitle:@"Friends"])
- //设置导航栏左侧按钮
- self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonSystemItemAdd target:nil action:nil];
- //设置导航栏右侧按钮
- self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ff_IconAdd.png"] style:UIBarButtonItemStyleDone target:self action:@selector(addFriends)];
- //修改当在下一级子视图中出现的左侧的返回按钮的显示内容
- UIBarButtonItem *backItem=self.navigationItem.backBarButtonItem;
- backItem.title=@"Add Friends";
- }
- -(void)addFriends{
- //通过push导航到另外一个子视图
- KCQQContactViewController *qqContactController=[[KCQQContactViewController alloc]init];
- [self.navigationController pushViewController:qqContactController animated:YES];
- }
- @end
3.在QQ联系人视图右侧导航中添加一个导航到公共账号的按钮
- //
- // KCQQContactViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCQQContactViewController.h"
- #import "KCPublicAccountViewController.h"
- @interface KCQQContactViewController ()
- @end
- @implementation KCQQContactViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //每次出栈都会销毁相应的子控制器
- NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers);
- [self setTitle:@"QQ Contact"];
- //self.title=@"QQ contact";
- //self.navigationItem.title=@"My QQ";
- UIBarButtonItem *back=[[UIBarButtonItem alloc]initWithTitle:@"QQ" style:UIBarButtonItemStyleDone target:nil action:nil];
- self.navigationItem.backBarButtonItem=back;
- self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Public Account" style:UIBarButtonItemStyleDone target:self action:@selector(gotoNextView)];
- }
- -(void)gotoNextView{
- KCPublicAccountViewController *publicAccountController=[[KCPublicAccountViewController alloc]init];
- [self.navigationController pushViewController:publicAccountController animated:YES];
- }
- @end
4.在公共账号视图中在导航栏右侧设置一个按钮用于直接返回根视图
- //
- // KCPublicNumberViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCPublicAccountViewController.h"
- @interface KCPublicAccountViewController ()
- @end
- @implementation KCPublicAccountViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //每次出栈都会销毁相应的子控制器
- NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers);
- self.title=@"Public Account";
- self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Add Friends" style:UIBarButtonItemStyleDone target:self action:@selector(gotoAddFriends)];
- }
- -(void)gotoAddFriends{
- //直接跳转到根控制器,也可以使用- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 方法
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- @end
UINavigationController默认显示一个根控制器,这个根视图必须指定(前面我们说过 UINavigationController和UITabBarController类似仅仅作为导航容器,本身并不会显示视图),通过根控制器导航到 其他下一级子视图。
在子视图中可以通过navigationController访问导航控制器,同时可以通过navigationController的childViewControllers获得当前栈中所有的子视图(注意每一个出栈的子视图都会被销毁)。
UINavigationController导航是通过上方导航栏进行的(类似的UITabBarController是通过下方 UITabBar进行导航),每个放到UINavigationController栈中的子视图都会显示一个导航栏,可以通过子控制器(包括根控制器) 的navigationItem访问这个导航栏,修改其左右两边的按钮内容。
默认情况下除了根控制器之外的其他子控制器左侧都会在导航栏左侧显示返回按钮,点击可以返回上一级视图,同时按钮标题默认为上一级视图 的标题(注意必须是通过[self title:@””]设置的标题,使用self.title设置则不能显示在返回按钮中)可以通过backBarButtonItem修改。下一级子视图 左侧返回按钮上的标题的显示优先级为: 导航栏返回按钮backBarButtonItem的标题(注意不能直接给backBarButtonItem的标题赋值,只能重新给 backBarButtonItem赋值)、导航栏navigationItem的标题,视图控制器标题(必须通过setTitle设置不能通 过.title属性设置)。