用UITabBarController可以容许用户在不同的screen切换。
而UINavigationController的作用是,容许用户在相关联的screen之间移动。
UINavigationController会维护一个screen的stack,每一个screen是一个view controller的View,而UINavigationController的stack里面是一组view controller,当一个view controller在stack顶的时候,它是可见的。你可以访问UINavigationController的topViewController或者visibleViewController属性来访问栈顶的view controller
当你初始化一个UINavigationController,并给他一个view controller,这个view controller是UINavigationController的root view controller。
当一个view controller入栈的时候,他的view从右边滑入。而当一个view controller出栈的时候,他的view从右边滑出。
像UITabBarController一样,UINavigationController也有一个view controller的数组。root view controller是数组的第一个对象。
UINavigationController是UIViewController的子类,它也有view,它的view有两个子view,UINavigationBar和topViewController的View。
从一个view controller到另一个,也就是把另一个入栈,用UINavigationController的
- pushViewController:animated: 出栈一个view controller用
- popViewControllerAnimated:, 也可以用– popToViewController:animated:
如果要从上一个 view controller传递值给下一个view controller,在prepareForSegue里面,取得下一个view controller的handler。直接赋值。如果下一个view controller对值进行了改变,那么要做一个model,让这个view controller改model的值。
当UINavigationController切换screen的时候,viewWillDisppear和viewWillAppear这2个消息会被发送。在栈顶会被出栈的view controller会发送viewWillDisppear,会变成栈顶的发送viewWillAppear。
UINavigationBar
UIViewController 有一个navigationItem属性,UINavigationBar会显示当前栈顶的UIViewController的navigationItem。
navigationItem缺省是nil值,他有4个主要可以定制的属性:
- leftBarButtonItem
- rightBarButtonItem
- titleView
- title