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的順序是與數組相反的
}