以一个UINavigationController作为根视图控制器,然后通过pushViewController函数实现多级跳转,以及通过popViewControllerAnimate d函数跳到上一个视图界面,popToViewController函数跳到指定视图界面,popToRootViewControllerA nimated函数跳到根视图界面。
这种情况下一般用UINavigationController作为window的跟视图控制器,并以一个ViewController作为UINavigationController的根视图控制器,这样ViewController就可以自由添加导航条并进行各种设置了,这里ViewController是一个UITableViewController:
一.导航视图UINavigationController

-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//初始化window
self.window = [[UIWindow alloc]init];
// window跟屏幕一样大
self.window.frame = [UIScreenmainScreen].bounds;
// window背景色
self.window.backgroundColor = [UIColorwhiteColor];
//表格菜单
MainTableViewController *mainVC =[[MainTableViewController alloc] init];
//根导航
UINavigationController *rootNavVC =[[UINavigationController alloc]initWithRootViewController:mainVC];
//跟导航作为window的跟控制器
self.window.rootViewController =rootNavVC;
return YES;
}
之后在ViewController中的viewDidLoad函数中就可以添加导航栏的各种设置了,标题,按钮等:
- (void)viewDidLoad{
[super viewDidLoad];
//标题
self.title = @"蒋信厚的Demo";
//导航条按钮组件
self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:selfaction:@selector(selector)];
//导航栏背景色
self.navigationController.navigationBar.backgroundColor = [UIColororangeColor];
//导航栏按钮组件的颜色
self.navigationController.navigationBar.tintColor = [UIColorredColor];
//标题颜色
[self.navigationController.navigationBarsetTitleTextAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15],
NSForegroundColorAttributeName:[UIColorwhiteColor]}];
}
导航视图下的页面跳转
[self.navigationControllerpushViewController:newViewController animated:YES];
// 获取当前视图控制器的index
NSInteger index= [self.navigationController.viewControllersindexOfObject:self];
//根据index获取历史视图
SLPatientDataController*diseaseVC= [self.navigationController.viewControllersobjectAtIndex:index-1];
[self.navigationControllerpopToViewController:diseaseVCanimated:YES];
[self.navigationControllerpopToRootViewControllerAnimated:YES];
2.底部导航条UITabBarController:
典型的底部导航结构如下,UITabBarController内放置多个自定义的UINavigationController,同时将每个子页面的根视图UIViewController作为导航视图UINavigationController的rootViewController,从而实现底部导航视图的分栏导航和每一栏里面的多级跳转:
三级类的示例如下:
UITabBarController
//
// MainTabBarController.h
// JXHDemo
//
// Created by Xinhou Jiang on 16/7/21.
// Copyright © 2016年 Jiangxh. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainTabBarController : UITabBarController
@end
//
// MainTabBarController.m
// JXHDemo
//
// Created by Xinhou Jiang on 16/7/21.
// Copyright © 2016年 Jiangxh. All rights reserved.
//
#import "MainTabBarController.h"
#import "TabNavController.h"
#import "MainTableViewController.h"
#import "FoundationViewController.h"
#import "MyDemoViewController.h"
@interface MainTabBarController ()
@end
@implementation MainTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.UIKit
MainTableViewController *uikitVC = [[MainTableViewController alloc]init];
[self addTabBarChildVC:uikitVC title:@"UIKit" icon:@"tabbar_home"];
// 2.Foundation
FoundationViewController *foundationVC = [[FoundationViewController alloc]init];
[self addTabBarChildVC:foundationVC title:@"Foundation" icon:@"tabbar_home"];
// 3.Demo
MyDemoViewController *demoVC = [[MyDemoViewController alloc]init];
[self addTabBarChildVC:demoVC title:@"Demo" icon:@"tabbar_home"];
// 底部导航栏选中后的文字颜色
self.tabBar.tintColor = RGBColor(28, 190, 164);
}
/**
* 添加tabbar子控制器
*/
- (void) addTabBarChildVC:(UIViewController*)childVC title:(NSString *)title icon:(NSString *)icon {
// 导航跟控制器
TabNavController *navController = [[TabNavController alloc]initWithRootViewController:childVC];
// 底部导航标题
navController.tabBarItem.title = title;
// 底部导航图标
navController.tabBarItem.image = [UIImage imageNamed:icon];
// 将当前子控制器添加到底部导航控制器
[self addChildViewController:navController];
}
@end
UINavigationController
//
// TabNavController.h
// JXHDemo
//
// Created by Xinhou Jiang on 16/7/21.
// Copyright © 2016年 Jiangxh. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface TabNavController : UINavigationController
@end
//
// TabNavController.m
// JXHDemo
//
// Created by Xinhou Jiang on 16/7/21.
// Copyright © 2016年 Jiangxh. All rights reserved.
//
#import "TabNavController.h"
@interface TabNavController ()
@end
@implementation TabNavController
- (void)viewDidLoad {
[super viewDidLoad];
// 顶部导航栏的背景色
self.navigationBar.barTintColor = RGBColor(28, 190, 164);
// 导航栏标题文字颜色和大小
[self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0],NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
@end
UIViewController
自定义。
效果如下: