纯代码实现
1、AppDelegate类
//.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController* viewController;
@end
//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc]init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
2、FKViewController类
#import "FKViewController.h"
@interface FKViewController ()
@end
@implementation FKViewController
// 记录当前是添加第几个UINavigationItem的计数器
NSInteger count;
UINavigationBar *navigationBar;
- (void)viewDidLoad
{
[super viewDidLoad];
count = 1;
// 创建一个导航栏
navigationBar = [[UINavigationBar alloc]
initWithFrame:CGRectMake(0, 20, 320, 44)];
// 把导航栏添加到视图中
[self.view addSubview:navigationBar];
// 调用push方法添加一个UINavigationItem
[self push];
}
-(void)push
{
// 把导航栏集合添加入导航栏中,设置动画打开
[navigationBar pushNavigationItem:
[self makeNavItem] animated:YES];
count++;
}
-(void)pop
{
// 如果还有超过2个的UINavigationItem
if(count > 2)
{
count--;
// 弹出最顶层的UINavigationItem
[navigationBar popNavigationItemAnimated:YES];
}
else
{
// 使用UIAlertView提示用户
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:@"只剩下最后一个导航项,再出栈就没有了"
delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
}
- (UINavigationItem*) makeNavItem
{
// 创建一个导航项
UINavigationItem *navigationItem = [[UINavigationItem alloc]
initWithTitle:nil];
// 创建一个左边按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(push)];
// 创建一个右边按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(pop)];
//设置导航栏内容
navigationItem.title = [NSString stringWithFormat:
@"第【%ld】个导航项"
, count];
//把左右两个按钮添加入导航栏集合中
[navigationItem setLeftBarButtonItem:leftButton];
[navigationItem setRightBarButtonItem:rightButton];
switch(count%4)
{
case 0:
[self.view setBackgroundColor:[UIColor redColor]];
break;
case 1:
[self.view setBackgroundColor:[UIColor orangeColor]];
break;
case 2:
[self.view setBackgroundColor:[UIColor blueColor]];
break;
case 3:
[self.view setBackgroundColor:[UIColor yellowColor]];
break;
default:
break;
}
return navigationItem;
}
@end