源码下载地址:http://download.csdn.net/detail/liu537192/8495011
效果图:
核心代码:
//
// JLAppDelegate.m
// 06-导航控制器
//
// Created by XinYou on 15-3-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLAppDelegate.h"
#import "JLOneViewController.h"
@implementation JLAppDelegate
- (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];
// 创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] init];
// 设置导航控制器的颜色
nav.view.backgroundColor = [UIColor grayColor];
// 设置window的根控制器为导航控制器
self.window.rootViewController = nav;
// 创建第1个控制器
JLOneViewController *one = [[JLOneViewController alloc] init];
// 把第1个控制器压入栈中
[nav pushViewController:one animated:YES];
[self.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
//
// JLOneViewController.m
// 06-导航控制器
//
// Created by XinYou on 15-3-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLOneViewController.h"
#import "JLTwoViewController.h"
@interface JLOneViewController ()
- (IBAction)jumpToNext;
@end
@implementation JLOneViewController
- (void)viewDidLoad{
[super viewDidLoad];
// 设置当前界面导航条的标题
self.navigationItem.title = @"第一个控制器";
// 设置当前界面导航条左边的按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
// 设置当前界面导航条右边的按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
// 设置下一个界面导航条左边的返回按钮(注意:是下一个界面)
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
}
- (IBAction)jumpToNext {
JLTwoViewController *two = [[JLTwoViewController alloc] init];
// 把第2个控制器压入栈中
[self.navigationController pushViewController:two animated:YES];
}
@end
//
// JLTwoViewController.m
// 06-导航控制器
//
// Created by XinYou on 15-3-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLTwoViewController.h"
#import "JLThreeViewController.h"
@interface JLTwoViewController ()
- (IBAction)jumpToNext;
@end
@implementation JLTwoViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationItem.title = @"第二个控制器";
// 设置下一个界面导航条左边的返回按钮
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
}
- (IBAction)jumpToNext {
JLThreeViewController *three = [[JLThreeViewController alloc] init];
// 把第3个控制器压入栈中
[self.navigationController pushViewController:three animated:YES];
}
@end
//
// JLThreeViewController.m
// 06-导航控制器
//
// Created by XinYou on 15-3-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLThreeViewController.h"
@interface JLThreeViewController ()
- (IBAction)backToOne;
- (IBAction)backToTwo;
@end
@implementation JLThreeViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationItem.title = @"第三个控制器";
}
- (IBAction)backToOne {
// 移除栈里面所有的控制器,直到只剩一个控制器
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (IBAction)backToTwo {
// 移除栈顶的控制器
[self.navigationController popViewControllerAnimated:YES];
}
@end