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

来源:互联网 发布:西安淘宝客服招聘 编辑:程序博客网 时间:2024/05/16 09:49

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

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

1.在APPDelegate中设置UITabBarController的代理

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

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


1.

@interface AppDelegate ()<UITabBarControllerDelegate>


/**

 *  @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){

        

    }

    */

}


阅读全文
0 0
原创粉丝点击