UINavigationController在多视图管理中的是经常用到的,它以栈的形式保存多有的屏幕的信息。这里的栈是一个数组对象,保存的都是UIViewController对象。一个UIViewController对象的视图对应一个屏幕,只有位于栈顶的UIViewController对象其视图才是可见的。
UINavigationController是UIViewController的子类,所以UINavigationController也有自己的视图。
将某个视图控制器压入UINavigationController对象的栈时,新加入的视图控制器的视图会从窗口右侧推入。返回时UINavigationController对象会移出位于栈顶的视图控制器,其视图也会从窗口的右侧推出。
接着我们就以之前的UITableView为基础添加UINavigationController
选择AppDelegate.m文件,添加修改代码如下
1 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
运行可以看到顶部多出一部分称之为UINavigationBar

回到ViewController.m文件 可以在UINavigationController上添加一个标题
1 | self.navigationItem.title = @"TableView"; |

- 添加左右按钮
1 | UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply |
- 这种方式添加的按钮有很多种类型
| UIBarButtonSystemItemDone | ![]() |
UIBarButtonSystemItemCancel |
|---|---|---|
| UIBarButtonSystemItemEdit | ![]() |
UIBarButtonSystemItemSave |
| UIBarButtonSystemItemAdd | ![]() |
UIBarButtonSystemItemCompose |
| UIBarButtonSystemItemReply | ![]() |
UIBarButtonSystemItemAction |
| UIBarButtonSystemItemOrganize | ![]() |
UIBarButtonSystemItemBookmarks |
| UIBarButtonSystemItemSearch | ![]() |
UIBarButtonSystemItemRefresh |
| UIBarButtonSystemItemStop | ![]() |
UIBarButtonSystemItemCamera |
| UIBarButtonSystemItemTrash | ![]() |
UIBarButtonSystemItemPlay |
| UIBarButtonSystemItemPause | ![]() |
UIBarButtonSystemItemRewind |
| UIBarButtonSystemItemFastForward | ![]() |
1111 |
在action中可以添加按钮的响应事件,当然按钮图标的本意跟响应事件我觉得应该是对应的。
- 也可以添加自己的图片
1 | - (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action; |
- 文字按钮
1 | - (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action; |
当要A视图切换到B视图是这个时候就可以用UINavigationController的push方法
1 | - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; |
- 前面说到
UITableView有一个didSelectRowAtIndexPath方法,点击cell实现视图跳转。
1 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |









