//导航控制器UINavigationController 是管理控制器的控制器,导航栏(屏幕顶部部分),页面之间的跳转都需要借助它来实现
//将第一个页面指定为导航控制器的根视图
RootViewController *root = [[RootViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
//将导航控制器指定为window的根视图
self.window.rootViewController = nav;
//对导航控制器的设置分两个部分(外观,内容)外观包括背景色,背景图片是否半透明 内容部分包括标题,左侧按钮,右侧按钮,通常我们将外观部分写在appdelegate里面
//设置背景色
//外观部分用navigationBar来控制
nav.navigationBar.barTintColor = [UIColor greenColor];
//nav.navigationBar.backgroundColor = [UIColor greenColor];
//取消半透明效果
nav.navigationBar.translucent = NO;
//设置背景图片
//[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarMetrics:UIBarMetricsDefault];
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航栏的内容部分
//设置标题
//控制内容用navigationItem
self.navigationItem.title = @"首页";
//
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
lable.text = @"首页";
lable.font = [UIFont boldSystemFontOfSize:25];
lable.textColor = [UIColor whiteColor];
lable.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = lable;
//添加左侧按钮(常用三种方式)
//self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(touchLeftButton)];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// button.frame = CGRectMake(0, 0, 90, 40);
// [button setImage:[UIImage imageNamed:@"shezhi"] forState:UIControlStateNormal];
// [button setTitle:@"设置" forState:UIControlStateNormal];
// [button addTarget:self action:@selector(touchLeftButton) forControlEvents:UIControlEventTouchUpInside];
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shezhi"] style:UIBarButtonItemStyleDone target:self action:@selector(touchLeftButton)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xie"] style:UIBarButtonItemStyleDone target:self action:@selector(youchRightButton)];
//隐藏自带的返回按钮用这句
self.navigationItem.hidesBackButton = YES;
NSArray *arr = [self.navigationController viewControllers];
}
-(void)touchLeftButton
{
NSLog(@"点击了左侧的按钮");
}
-(void)youchRightButton
{
NSLog(@"点击了右侧按钮");
SecondViewController *second = [[SecondViewController alloc] init];
//借助导航控制器实现跳转
second.str = tf.text;
second.myDelegate = self;
[self.navigationController pushViewController:second animated:YES];
}
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"第二页";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(touchBack)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xiayiye"] style:UIBarButtonItemStylePlain target:self action:@selector(touchRightButton)];
NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);
}
-(void)touchRightButton
{
ThirdViewController *third = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:third animated:YES];
}
-(void)touchBack
{
//从哪来回哪去 直接返回上一页
// [self.navigationController popToViewController: animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);
//查看栈顶的元素
NSLog(@"%@",[self.navigationController topViewController]);
//查看当前显示的controller
NSLog(@"%@",[self.navigationController visibleViewController]);
}
@end