UINavigaiongController的基本使用(1)
需求
- xib 創建控制器(待更新)
- 創建導航控制器,將OneViewController控制器設置為棧底控制器("基本使用",有實現)
- 點擊OneViewController 的Button,跳轉到TwoViewController
- 返回控制器
4.1 返回上個控制器
4.2 跳轉到指定的控制器
4.3 返回根控制器
層次結構
@interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer,
UITraitEnvironment, UIContentContainer, UIFocusEnvironment>
@interface UINavigationController : UIViewController
//UINavigationController的方法和屬性
- (void)pushViewController:(UIViewController *)viewController animated:
(BOOL)animated;
//返回上個控制器
- (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 {
//創建第二個控制器
//TowViewController 是自定義的UIViewController
TowViewController * twoCon = [[TowViewController alloc]init];
//獲取導航控制器,調用導航控制器的pust方法
[self.navigationController pushViewController:twoCon animated:YES];
}
@implementation ThreeViewController
- (IBAction)back:(id)sender {
//獲取導航控制器
//返回上個控制器,移除棧頂的控制器
//[self.navigationController popViewControllerAnimated:YES];
//返回指定的控制
//報錯: Tried to pop to a view controller that doesn't exist
//因為: OneViewControlle 是新建的控制器,并沒有在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];
}
效果圖
點擊按鈕,跳轉控制器