pushViewController:animated //进入下一个视图控制器
popViewControllerAnimated //返回上一个视图控制器
popToViewController:animated //返回到指定的视图控制器
popToRootViewControllerAnimated //返回到根视图控制器
定制UINavigationBar
ois7默认的navigationBar 高度是64
如果将navigationBar的透明度关闭,navigationBar的高度将会变为44
修改UINavigationBar的背景图片,背景颜色 UINavigationItem的使用
传值(第二个视图控制器获得第一个视图控制器的部分信息:
属性传值
单例传值(类只有一个实例,也是一种常用的设计模式)
代理传值
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//设置导航栏不透明
[self.navigationController.navigationBar setTranslucent:NO];
self.title = @"标题";
// //设置导航栏样式
// [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
// //设置导航栏颜色
// [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.bmp"] forBarMetrics:UIBarMetricsDefault];
//设定导航栏上的视图back 只会取其样式 不去我设的功能
// self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"你猜" style: UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithTitle:@"AAA" style:UIBarButtonItemStylePlain target: self action:@selector(buttonClicked:)],[[UIBarButtonItem alloc] initWithTitle:@"AAA" style:UIBarButtonItemStylePlain target: self action:@selector(buttonClicked:)], nil];
UIButton * label = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
label.backgroundColor = [UIColor cyanColor];
[label addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//titleView本身是一个空指针,必须设置个东西给它,让这个指针去指向它
NSLog(@"%@",self.navigationItem.titleView);
self.navigationItem.titleView = label;
[label release];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 20, 200, 150);
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"嘻嘻,我是按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClicked:(UIButton *)button
{
//2.创建一个要入栈的视图控制器
SecondViewController *secondController = [[SecondViewController alloc] init];
//3.获得当前的navigationController,利用它推出新的视图控制器
[self.navigationController pushViewController:secondController animated:YES];
[secondController release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.6 alpha:0.6];
[self.navigationController.navigationBar setTranslucent:NO];
self.title = @"四大美女";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BBB" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
// UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
// button.frame = CGRectMake(50, 150, 200, 150);
// button.backgroundColor = [UIColor yellowColor];
// [button setTitle:@"我也是按钮" forState:UIControlStateNormal];
// [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:button];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 250, 100)];
label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];
[label release];
}
- (void)buttonClicked:(UIButton *)button
{
ThirdViewController * thirdController = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdController animated:YES];
[thirdController release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController.navigationBar setTranslucent:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"DDD" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
self.view.backgroundColor = [UIColor grayColor];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 150, 200, 150);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"按钮。。按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClicked:(UIButton *)button
{
//返回到根视图控制器
// [self.navigationController popToRootViewControllerAnimated:YES];
//返回上一个视图控制器
// [self.navigationController popViewControllerAnimated:YES];
//返回到栈内的某一个视图
// UIViewController * vc = [self.navigationController.viewControllers objectAtIndex:1];
// [self.navigationController popToViewController: vc animated:YES];
FiveViewController * fiveController = [[FiveViewController alloc] init];
[self.navigationController pushViewController:fiveController animated:YES];
[fiveController release];
}