未调用UITabBarControllerDelegate操作

时间:2014-06-19 20:21:03

标签: ios objective-c cocoa-touch

看起来很多新的iOS开发人员都遇到了这个客观c类的问题。 我试图检测5个标签项中的哪一个(家庭,产品,我的购物车,搜索,更多)。 根据索引(0,2,4),我想显示根视图,如果有任何其他视图堆叠在它上面。 这是AppDelegate.h的代码:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {
    UITabBarController *tabBarController;
}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;
@end

AppDelegate.h代码:

@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.tabBarController.delegate = self;
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"action called.");
}

1 个答案:

答案 0 :(得分:0)

您可以通过在标签栏控制器的委托方法中调用所选视图控制器的-popToRootViewControllerAnimated:方法来完成此操作:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger vcIndex = [[tabBarController viewControllers] indexOfObject:viewController];
    BOOL shouldPop = (vcIndex == 0 || vcIndex == 2 || vcIndex == 4);
    if (shouldPop && [viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
    }
}

请记住,这可能不是最好的用户体验 - 用户通常希望他们可以在选项卡之间切换,而不会在返回其他选项卡时丢失其位置或状态。弹出到根视图控制器通常会丢弃该状态,这可能导致挫败感。

相关问题