UINavigaiongController的基本使用(1)
需求
- xib 創(chuàng)建控制器(待更新)
- 創(chuàng)建導(dǎo)航控制器,將OneViewController控制器設(shè)置為棧底控制器("基本使用",有實(shí)現(xiàn))
- 點(diǎn)擊OneViewController 的Button,跳轉(zhuǎn)到TwoViewController
- 返回控制器
4.1 返回上個(gè)控制器
4.2 跳轉(zhuǎn)到指定的控制器
4.3 返回根控制器
層次結(jié)構(gòu)
@interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer,
UITraitEnvironment, UIContentContainer, UIFocusEnvironment>
@interface UINavigationController : UIViewController
//UINavigationController的方法和屬性
- (void)pushViewController:(UIViewController *)viewController animated:
(BOOL)animated;
//返回上個(gè)控制器
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
//返回指定控制器
- (nullable NSArray<UIViewController *> *)popToViewController:
(UIViewController *)viewController animated:(BOOL)animated;
//返回根控制器
- (nullable NSArray<UIViewController *> *)
popToRootViewControllerAnimated:(BOOL)animated;
@property(nullable, nonatomic,readonly,strong)
UINavigationController *navigationController;
代碼
@implementation OneViewController
- (IBAction)jumpTwo:(id)sender {
//創(chuàng)建第二個(gè)控制器
//TowViewController 是自定義的UIViewController
TowViewController * twoCon = [[TowViewController alloc]init];
//獲取導(dǎo)航控制器,調(diào)用導(dǎo)航控制器的pust方法
[self.navigationController pushViewController:twoCon animated:YES];
}
@implementation ThreeViewController
- (IBAction)back:(id)sender {
//獲取導(dǎo)航控制器
//返回上個(gè)控制器,移除棧頂?shù)目刂破? //[self.navigationController popViewControllerAnimated:YES];
//返回指定的控制
//報(bào)錯(cuò): Tried to pop to a view controller that doesn't exist
//因?yàn)? OneViewControlle 是新建的控制器,并沒(méi)有在UINavigationController里面
//OneViewController * oneCon = [[OneViewController alloc]init];
//[self.navigationController popToViewController:oneCon animated:YES];
//OneViewController* oneCon = self.navigationController.viewControllers[0];
//[self.navigationController popToViewController:oneCon animated:YES];
//返回根控制器
[self.navigationController popToRootViewControllerAnimated:YES];
}
效果圖