Appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
UINavigationController *navigationCtonroller =[[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
[self.window setRootViewController:navigationCtonroller];
return YES;
}
FistViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.title = @"First";
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
//设置系统图片,设置单个view
// self.navigationItem.leftBarButtonItem = leftButton;
//自定义itembutton
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
label.text = @"应用商店";
label.backgroundColor = [UIColor grayColor];
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:label];
[label targetForAction:@selector(pushController) withSender:self];
//设置多个itmesButton
self.navigationItem.leftBarButtonItems = @[leftButton,customButton];
//设置导航控制器的颜色
// self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
//设置导航控制器的背景图片
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"buttonPost"] forBarMetrics:UIBarMetricsDefault];
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
pushButton.frame = CGRectMake(0, 100, 100, 30);
pushButton.backgroundColor = [UIColor greenColor];
[pushButton setTitle:@"push" forState:UIControlStateNormal];
[pushButton addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushButton];
}
- (void)pushController {
NSLog(@"hehhe");
//这里如果SecondViewController没有设置背景色,没有做其他操作,在跳转时,跳转动画将发生卡顿现象
[self.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"Second";
self.view.backgroundColor = [UIColor blueColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"POP" style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
self.navigationItem.leftBarButtonItem = item;
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)push{
[self.navigationController pushViewController:[[ThridViewController alloc] init] animated:YES];
}
- (void)pop {
[self.navigationController popViewControllerAnimated:YES];
}
ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"Third";
self.view.backgroundColor = [UIColor greenColor];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
self.navigationItem.leftBarButtonItem = rightItem;
}
- (void)push {
//跳转到根视图
// [self.navigationController popToRootViewControllerAnimated:true];
//销毁当前视图
// [self.navigationController popViewControllerAnimated:YES];
//获取导航控制器下所有的视图控制器
NSLog(@"%@", self.navigationController.viewControllers);
//跳转到指定视图控制器
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}