美文网首页
(2)UINavigaiongController实现控制器直接

(2)UINavigaiongController实现控制器直接

作者: js_huh | 来源:发表于2016-07-21 23:23 被阅读50次

    UINavigaiongController的基本使用(1)
    需求

    1. xib 创建控制器(待更新)
    2. 创建导航控制器,将OneViewController控制器设置为栈底控制器("基本使用",有实现)
    3. 点击OneViewController 的Button,跳转到TwoViewController
    4. 返回控制器
      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];
        
    }
    

    效果图

    点击按钮,跳转控制器

    相关文章

      网友评论

          本文标题:(2)UINavigaiongController实现控制器直接

          本文链接:https://www.haomeiwen.com/subject/rgbbjttx.html