1.简介
UINavigationController:是iOS常见的一种容器型Controller。官方文档上给出的注释如下
UINavigationController manages a stack of view controllers and a navigation bar.
It performs horizontal view transitions for pushed and popped views while keeping the navigation bar in sync.
Most clients will not need to subclass UINavigationController.
If a navigation controller is nested in a tabbar controller, it uses the title and toolbar attributes of the bottom view controller on the stack.
UINavigationController is rotatable if its top view controller is rotatable.
Navigation between controllers with non-uniform rotatability is currently not supported.
意思是说,UINavigationController管理着一个viewController的栈和一个navigation bar(navigation bar后面会说)。管理着出于同一层次的viewController的view,包括压栈和出栈(viewController显示滑进和隐藏滑出界)的转场动画,同时还同步管理着顶部navigation bar的状态。大部分的app都不需要实现一个它的子类(对于一般的功能,都具有支持)。如果一个UINavigationController的实例嵌套在一个UITabbarController的实例中,它将使用栈底的viewController的标题和toolbar的属性。UINavigationController的可旋转是和栈顶的viewController保持一致的。现在还不支持同一层次的viewController旋转类型不一致的压栈出栈。
2.常见属性和方法
- (
instancetype
)initWithRootViewController:(UIViewController *)rootViewController;
以一个viewController为栈底,实例化一个navigation viewcontroller
- (
void
)pushViewController:(UIViewController *)viewController animated:(
BOOL
)animated;
展示一个viewcontroller,压栈操作(可以有动画效果)。
- (
nullable
UIViewController *)popViewControllerAnimated:(
BOOL
)animated;
结束并隐藏一个viewcontroller(dealloc),出栈操作(可以有动画效果),返回出栈的viewcontroller
- (
nullable
NSArray<
__kindof
UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(
BOOL
)animated;
结束并隐藏 在栈中位于一个特殊的viewcontroller之前所有的viewcontroller,并返回
- (
nullable
NSArray<
__kindof
UIViewController *> *)popToRootViewControllerAnimated:(
BOOL
)animated;