1.創(chuàng)建
通過xib創(chuàng)建
通過代碼創(chuàng)建
一個UINavigationcontroller包括 navigation bar,可選的navigation toolbar,RootViewController.
2.導(dǎo)航棧
有四個方法
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];//1.
DetailsViewController *detailsViewController = [[DetailsViewController alloc]
initWithNibName:@"DetailsViewController" bundle:nil];
[self.navigationController pushViewController:detailsViewController];
[detailsViewController release];
}
3.配置Navigation bar
可能大家想直接訪問navigationcontroller 的navigation bar。但是通常我們不這樣做。而是維護(hù)每個viewcontroller的 navigation item。
這里不要將navigation item 與 navigation bar 混淆,navigation item不是UIView的子類。它是一個用來更新navigtion bar的存儲信息的類。
還是上代碼說明:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
Person *person;
// Some code that sets person based on the particular cell that was selected
DetailsViewController *detailsViewController = [[DetailsViewController alloc]
initWithNibName:@"DetailsViewController" bundle:nil];
detailsViewController.navigationItem.title = person.name;
[self.navigationController pushViewController:detailsViewController];
[detailsViewController release];
}
detailsViewController.navigationItem.title = person.name;這句話的意思就是把二級界面的導(dǎo)航標(biāo)題設(shè)置成person.name
要注意兩點:1.我們并沒有直接操作navigation bar 2.在push 新的controller之前設(shè)置標(biāo)題
當(dāng)新的detailcontroller被push后,UINavigationController會自動更新navigation bar。
4.返回按鈕
默認(rèn)情況下,當(dāng)你將一個新的viewcontroller推入棧的時候,返回按鈕將顯示前一個頁面的controller的 navigation item的title。
如果想定制返回按鈕的標(biāo)題還有事件的話,可以用以下代碼。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
注意,這里的self是第一級的view controller。這樣的話第二級的頁面將顯示“Back”
5.左右按鈕
navigation item還有兩個屬性leftBarButtonItem rightBarButtonItem。
一般leftBarButtonItem只出現(xiàn)在RootviewController中使用,因為其他頁面一般都顯示一個返回按鈕。
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings"
style:UIBarButtonItemStylePlain target:self action:@selector(handleSettings)];
self.navigationItem.rightBarButtonItem = settingsButton;
[settingsButton release];
這會在右側(cè)添加一個“Setting”的按鈕,并觸發(fā)handleSetting事件。
6.在首頁隱藏Navigation Bar
在RootViewController.m中實現(xiàn)如下:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
這篇文章翻譯自http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/