NavigationController & TabBarController都是界面控制的方式,在使用上感觉很近似,因此就把它们的笔记放在一起了。
Navigation Bar
Navigation Bar:最常用的界面跳转控制方法之一
每个被管理的View Controller要提供:
- 内容
- 导航栏标题
- 导航栏上额外按键
- 工具栏(可选)按键
图示
navigation_interface
nav_controllers_objects
结构:管理的VC,公共界面、Delegate
Lifecycle
- Tip:传递数据建议用Segue方法
在代码中使用导航
创建
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; // Convenience method pushes the root view controller without animation.
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated ; // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.
跳转
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender ; // Interpreted as pushViewController:animated:
设置和控制
@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; // The current view controller stack.
@property(nonatomic,readonly) UINavigationBar *navigationBar; // The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
@property(null_resettable,nonatomic,readonly) UIToolbar ; // For use when presenting an action sheet.
界面定制
Demo:helloNavUI
UINavigationBar
UIBarButtonItem,UIBarButtonSystemItem
UITabBarController
一种分页的方法
图示
UITabBarController的结构
代码
//创建
UITabBarController *vc =[[UITabBarController alloc]init];
vc.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"HOME" image:@"first" tag:0];
//管理
[self.tabBarController setViewControllers:@"FirstViewController" animated:YES];
//add
[self.view addSubview:vc];
//选中
vc.selectedViewController =vc;
vc.selectedIndex = 1;