****HMAppDelegate.m
@implementation HMAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; HMOneViewController *oneVc = [[HMOneViewController alloc] init]; UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:oneVc]; self.window.rootViewController = navVc; [self.window makeKeyAndVisible]; return YES;}-(void)test1{ //1.创建一个导航控制器 //UINavigationController *navVc = [[UINavigationController alloc] init]; //2.设置导航控制器的子控制器 UIViewController *oneVc = [[UIViewController alloc] init]; oneVc.view.backgroundColor = [UIColor grayColor]; UIViewController *twoVc = [[UIViewController alloc] init]; twoVc.view.backgroundColor = [UIColor purpleColor]; //method 1 : 添加导航控制器的子控制器 //[navVc pushViewController:oneVc animated:YES]; //[navVc pushViewController:twoVc animated:YES]; //method 2 //navVc.viewControllers = @[oneVc]; //method 3 // [navVc addChildViewController:oneVc]; // [navVc addChildViewController:twoVc]; //self.window.rootViewController = navVc;}
******HMOneViewController.m
#import "HMOneViewController.h"#import "HMTwoViewController.h"@interface HMOneViewController ()- (IBAction)jumpTwoVc:(id)sender;@end@implementation HMOneViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //NSLog(@"%@",self.navigationController.viewControllers); //设置导航栏的内容 //设置标题 self.navigationItem.title = @"第一个控制器"; //设置导航栏左边的按钮 UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil]; self.navigationItem.leftBarButtonItem = leftBtn; //设置下一个控制器的返回按钮 //当前控制的navigationItem里的返回按钮是决定下一个控制器的返回按钮 self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//跳到第二个控制器- (IBAction)jumpTwoVc:(id)sender { HMTwoViewController *twoVc = [[HMTwoViewController alloc] init]; NSLog(@"%@",self.navigationController); [self.navigationController pushViewController:twoVc animated:YES];}@end
*******HMTwoViewController.m
#import "HMTwoViewController.h"#import "HMThreeViewController.h"@interface HMTwoViewController ()- (IBAction)backOneVc:(id)sender;- (IBAction)jumpThreeVc;@end@implementation HMTwoViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //NSLog(@"%@",self.navigationController.viewControllers); //设置标题 self.navigationItem.title = @"第二个控制器"; //设置返回按钮 self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BACK" style:UIBarButtonItemStylePlain target:nil action:nil]; //设置导航栏左边的按钮 //如果设置leftBarButtonItem ,之前那个返回无效 UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)]; self.navigationItem.leftBarButtonItem = leftBtnItem; //设置导航栏右边的按钮 UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil]; //self.navigationItem.rightBarButtonItem = search; UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:nil]; self.navigationItem.rightBarButtonItems = @[search,refresh]; }//返回上一个控制器-(void)back{ [self.navigationController popViewControllerAnimated:YES];}- (IBAction)backOneVc:(id)sender { [self.navigationController popViewControllerAnimated:YES];}- (IBAction)jumpThreeVc { //跳到第三个控制器 HMThreeViewController *threeVc = [[HMThreeViewController alloc] init]; [self.navigationController pushViewController:threeVc animated:YES];}@end
#import "HMThreeViewController.h"@interface HMThreeViewController ()- (IBAction)backOneVc:(id)sender;@end@implementation HMThreeViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //栈的所有控制器 NSLog(@"%@",self.navigationController.viewControllers); NSArray *array = [self.navigationController childViewControllers]; NSLog(@"%@",array); //设置标题View UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; self.navigationItem.titleView = btn; //设置返回按钮btn_back_normal UIButton *redBtn = [UIButton buttonWithType:UIButtonTypeCustom]; redBtn.bounds = CGRectMake(0, 0, 46, 31); [redBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_normal"] forState:UIControlStateNormal]; [redBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *redBackBtnItem = [[UIBarButtonItem alloc] initWithCustomView:redBtn]; self.navigationItem.leftBarButtonItem = redBackBtnItem; }//返回上一个控制咕噜-(void)back{ [self.navigationController popViewControllerAnimated:YES];}- (IBAction)backOneVc:(id)sender { //返回第一个控制器 //[self.navigationController popToRootViewControllerAnimated:YES]; //获取第一个控制器 UIViewController *oneVC = self.navigationController.viewControllers[0]; //返回指定控制器 [self.navigationController popToViewController:oneVC animated:YES];}@end