美文网首页
点击底部Tabbar某个item,返回到其对应的初始页

点击底部Tabbar某个item,返回到其对应的初始页

作者: 月夜芳华 | 来源:发表于2018-07-03 15:25 被阅读0次

因项目需求,需要将底部Tabbar点击后达到这样的效果:点击首页后,要求显示最初的首页界面,而非二级、三级界面等。

研究了下TabbarController的代理方法,找到了解决办法。思路:window的根视图是UITabBarController,UITabBarController的子viewControllers中放的是UINavgationController,所以,找到底部item的点击事件,将UINavgationController  Pop 到navgationController根视图即可,(前提是viewControllers中放的是UINavgationController)

1.在APPDelegate中设置UITabBarController的代理

2.遵循协议,实现协议的方法

3.通过UINavgationController  Pop 到navgationController的根视图

1.

@interface AppDelegate ()

/**

 *  @discussion  设置TabbarController,在此方法中设置TabbarController的代理

 */

2.

-(void)setupTabBarController {

    self.window.rootViewController.view.alpha = 0;

    RootTabViewController *tabbar = [[RootTabViewControlleralloc] init];

    self.window.rootViewController = tabbar;

    tabbar.delegate =self;    // 设置代理

}

3.

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

    BaseNavigationController *base = (BaseNavigationController *)viewController;

     [base popToRootViewControllerAnimated:NO];

    /* 可根据项目需求自行修改

    if (tabBarController.selectedIndex == 0) {

    }else if (tabBarController.selectedIndex == 1){

    }else if (tabBarController.selectedIndex == 2){

    }else if (tabBarController.selectedIndex == 3){

    }else if (tabBarController.selectedIndex == 4){

    }

    */

}

相关文章

网友评论

      本文标题:点击底部Tabbar某个item,返回到其对应的初始页

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