#import "AppDelegate.h"
#import "OneViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//初始化OneViewController
OneViewController *oneVC = [[OneViewController alloc]init];
//初始化导航控制器,并为导航控制器添加根视图
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:oneVC];
//如果有导航控制器,就将导航控制器设置为window的根视图控制器
self.window.rootViewController = navC;
//设置导航条的颜色 通栏
navC.navigationBar.barTintColor = [UIColor orangeColor];
//设置导航条上所有按钮的颜色
navC.navigationBar.tintColor = [UIColor redColor];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "OneViewController.h"
#import "DetailViewController.h"
#import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置标题
// self.navigationItem.title = @"我是导航条";
//可以自定义标题区域显示的内容或者控件
//分段控制器
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];
//系统给保护措施了。所以位置无论怎么变,总是显示在导航条的最中间
segment.frame = CGRectMake(0, 0, 200, 30);
//显示
self.navigationItem.titleView =segment;
//添加左边按钮(初始化)
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(myAddAction:)];
//将初始化好的左侧按钮赋给navigationITem的左侧按钮
self.navigationItem.leftBarButtonItem = leftBarButton;
//添加右侧按钮(初始化) //style:两个枚举值,一个粗体,一个细的
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"HanOBa" style:UIBarButtonItemStyleDone target:self action:@selector(myAddAction1:)];
//将初始化好的右侧按钮赋给导航条的右侧按钮
self.navigationItem.rightBarButtonItem = rightBarButton;
//此代码是让导航器上左右可以加多个控制器,一组按钮(是以数组的方式存在)
// self.navigationItem.rightBarButtonItems =@[leftBarButton,rightBarButton];
}
//点击跳转到详情界面
-(void)myAddAction:(UIBarButtonItem *)sender{
//初始化将要跳转到得视图控制器
DetailViewController *detailVC = [[DetailViewController alloc]init];
//将要跳转到的视图控制器入栈(让视图控制器受导航控制器管理)
//<#(UIViewController *)#>是将要入栈的控制器(将要显示的控制器)
//animated:<#(BOOL)#> 视图切换的时候是否需要动画
[self.navigationController pushViewController:detailVC animated:YES];
//目前受导航控制器管理的视图控制器,入栈实际上就是将一个视图控制器加到导航控制器的viewControllers数组中。目前所显示的视图控制器肯定是位于栈顶,也可以说是viewControllers数组的最后一个元素就是位于栈顶的视图控制器
NSLog(@"被管理的---%@",self.navigationController.viewControllers);
//位于栈顶的视图控制器
NSLog(@"栈顶---%@",self.navigationController.topViewController);
//目前正在显示的视图控制器
NSLog(@"正在显示---%@",self.navigationController.visibleViewController);
}
//HanOBa按钮的跳转方法
-(void)myAddAction1:(UIBarButtonItem *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "DetailViewController.h"
#import "TwoViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"详情界面";
//自定义一个左侧按钮,覆盖系统提供的返回按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Happy" style:UIBarButtonItemStylePlain target:self action:@selector(happyAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
//返回上级界面
-(void)backAction:(UINavigationController *)sender{
// [self.navigationController popViewControllerAnimated:YES];
NSLog(@"被管理者---%@",self.navigationController.viewControllers);
//返回到导航控制器的根视图控制器(也就是初始化的时候OneViewController)
//这句和上句二选一,否则出栈两次
[self.navigationController popToRootViewControllerAnimated:YES];
//返回指定的视图控制器
//这里的返回某个界面很复杂,我说不清楚。
[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
}
-(void)happyAction:(UINavigationController *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给第三个页面的导航条加一个标题
self.navigationItem.title = @"哒哒哒";
//创建一个右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"返回HanOBa" style:UIBarButtonItemStyleDone target:self action:@selector(dadadaAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
//回调方法
-(void)dadadaAction:(UINavigationController *)sender{
//[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
//这句和上面那句意义一样,不过这句是用下标法返回第i个视图的,上面的只能找到第一个和最后一个视图。
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end