美文网首页IOS OC UI相关iOS 开发
iOS之TabbarController和NavigationC

iOS之TabbarController和NavigationC

作者: 文艺情怀的程序猿 | 来源:发表于2018-06-14 11:10 被阅读0次

    现在绝大部分APP都采用TabbarController和NavigationController相结合的设计模式,也是iOS一种很经典的框架设计模式,下面我们就来说一下,具体是怎么实现的。

    TabbarController和NavigationController这种模式,一般有两种设计方案。①多个TabBarController的viewControllers 和一个NavigationController相结合,这种设计模式tabbar底部不会和NavigationController的Title相对应,所以不推荐这种设计模式。重点说下第二种设计方案②多个TabBarController的viewControllers + 多个NavigationController,这种设计模式tabbar上的标题会直接同步到NavigationController上.每个界面都有自己的导航控制器, 界面跳转也都有自己的栈,会更加灵活。

    第二种设计方案创建过程如下(关键代码):

    /**

    *  初始化所有的子控制器

    */

    - (void)setupAllChildViewControllers

    {

        //1.首页

        HomeViewController *home = [[HomeViewController alloc] init];

        [self setupChildViewController:home title:@"首页" imageName:@"首页" selectedImageName:@"首页_选中"];

        self.home = home;

    //    2.课堂

       ClassRoomController *classRoom = [[ClassRoomController alloc]init];

        [self setupChildViewController:classRoom title:@"课堂" imageName:@"课堂" selectedImageName:@"课堂_选中"];

        self.classRoom = classRoom;

    //   3.我的

        MineController *mine = [[MineController alloc] init];

        [self setupChildViewController:mine title:@"我的" imageName:@"我的" selectedImageName:@"我的_选中"];

        self.mine = mine;

    }

    /**

    *  初始化一个子控制器

    *

    *  @param childVc          需要初始化的子控制器

    *  @param title            标题

    *  @param imageName        图标

    *  @param selectedImageName 选中的图标

    */

    - (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName

    {

        // 1.设置控制器的属性

        childVc.title = title;

        // 设置图标

        childVc.tabBarItem.image = [UIImage imageNamed:imageName];

        // 设置选中的图标

        UIImage *selectedImage = [UIImage imageNamed:selectedImageName];

        childVc.tabBarItem.selectedImage = selectedImage;

        // 2.包装一个导航控制器

        UINavgationController *nav = [[UINavgationController alloc] initWithRootViewController:childVc];

        [self addChildViewController:nav];

        // 3.添加tabbar内部的按钮

        [self.customTarBar addTabBarButtonWithItem:childVc.tabBarItem];

    }

    相关文章

      网友评论

        本文标题:iOS之TabbarController和NavigationC

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