UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的APP中(如qq,系统相册等)都有用到。
1.创建第一个视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//创建第一个视图控制器,并用其创建导航控制器
FirstViewController *fvc = [[FirstViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:fvc];
//将导航控制器作为window的根视图控制器
self.window.rootViewController = nc;
[fvc release];
[nc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
2.设置标题及颜色
- (void)viewDidLoad {
[super viewDidLoad];
//设置标题,若不设置下个视图左边默认显示back
//若显示不完,在下个视图左边仍会显示默认back
self.title = @"firstViewController";
self.view.backgroundColor = [UIColor redColor];
[self createButtons];
}
3.在第一个视图上创建button 用于点击进入下一个视图
- (void)createButtons
{
NSArray *titles = @[@"next"];
for (NSUInteger i=0; i<titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 200, 50);
button.center = CGPointMake(self.view.frame.size.width/2, 100+80*i);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:titles[i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:30];
[button addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+i;
[self.view addSubview:button];
}
}
4.实现button的方法
- (void)clickHandle:(UIButton *)button
{
switch (button.tag) {
case 100:
{
//创建第二个视图控制器
SecondViewController *svc = [[SecondViewController alloc] init];
//将其压到导航控制器中
[self.navigationController pushViewController:svc animated:YES];
[svc release];
}
break;
default:
break;
}
}
5.创建一个UINavigationItem
- (void)customNavigationItem
{
//找到NavigationItem
UINavigationItem *item = self.navigationItem;
//设置标题,与上面self.title = @"second";等价
//最后设置的有效,会覆盖前面的设置
item.title = @"第二个";
//设置标题视图(任意的View)
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, 44, 44);
view.backgroundColor = [UIColor grayColor];
item.titleView = view;
[view release];
//创建UIBarButtonItem
//系统默认风格
UIBarButtonItem *barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(barButtonHandle:)];
barItem1.tag = 100;
//通过指定图片创建item
UIImage *image = [UIImage imageNamed:@"itemImage"];
//设置图片渲染效果
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barItem2 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(barButtonHandle:)];
barItem2.tag = 200;
//通过指定标题创建item
UIBarButtonItem *barItem3 = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStyleDone target:self action:@selector(barButtonHandle:)];
barItem3.tag = 300;
//自定义view创建item
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 44, 44);
button.titleLabel.adjustsFontSizeToFitWidth = YES;
[button setTitle:@"custom" forState:UIControlStateNormal];
[button addTarget:self action:@selector(barButtonHandle:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 400;
UIBarButtonItem *barItem4 = [[UIBarButtonItem alloc] initWithCustomView:button];
//不建议使用
//item.backBarButtonItem = barItem2;
//设置单个
//item.leftBarButtonItem = barItem1;
//设置多个
item.leftBarButtonItems = @[barItem1,barItem2,barItem3,barItem4];
[barItem1 release];
[barItem2 release];
[barItem3 release];
[barItem4 release];
//右边设置方式与左边一样,但是item的顺序是与数组相反的
}