有没有为 tabBar willSelectItem 服务的委托?

Is there any delegate that serves the purpose of tabBar willSelectItem?

我需要在用户选择 UI 选项卡项时执行一些 UI 任务。以下代表有空,

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

虽然,为了回答我的特定问题,内部 UI 转换问题在这里并不重要,但我仍然共享一个概述代码片段。

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [delegate.rootTabBarController showConsentErrorPage];
}

但是,我在这个委托中的 UI 任务在转换时显示了一个小故障,因为它在选项卡已经显示之后才开始工作。我想在 UI 可见之前先执行 UI 任务。任何这样的技巧代表可以解决这个问题?

这个可能对你有帮助(没有额外的信息,我真的不能说)。

  • 子类 UITabBarController 符合 <UITabBarControllerDelegate>
  • 在那个新的自定义 UITabBarController 中,一定要在 viewDidLoad
  • 中设置 self.delegate = self;
  • 实施shouldSelectViewController
  • 如果该控制器是您“需要同意才能查看”的视图控制器,
    • 检查用户是否已经同意
    • 如果是,return YES(即允许选择标签)
    • 如果没有,请出示您的“征求同意”控制器和 return NO
    • 如果用户同意,导航到该选项卡

这是一些示例代码...

使用此选项,我们会显示“征求同意”控制器,并且仅在用户选择“是”时导航至“需要同意才能查看”选项卡:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    if ([viewController isKindOfClass:NeedsConsentViewController.class]) {
        
        NeedsConsentViewController *vc = (NeedsConsentViewController *)viewController;
        
        // whatever you're using to track the user's consent
        if (vc.hasConsent) {
            // allow the tab to be selected
            return YES;
        }
        
        // configure / instantiate your "Consent" view controller
        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Yes/No"
                                     message:@"Need your consent..."
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* okButton = [UIAlertAction
                                   actionWithTitle:@"Yes"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
            
            // however you're setting your user consent tracking
            vc.hasConsent = YES;
            
            // show that tab
            [self setSelectedViewController:vc];
            
        }];
        
        [alert addAction:okButton];
        
        UIAlertAction* noButton = [UIAlertAction
                                   actionWithTitle:@"No"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
            
            // user said NO... nothing else to do

        }];
        
        [alert addAction:noButton];
        
        [self presentViewController:alert animated:YES completion:nil];
        
        // don't show the tab
        return NO;
    }
    
    // all other tabs
    return YES;
}

使用此选项,我们显示“征求同意”控制器 导航到其后面的“需要同意才能查看”选项卡.如果用户回答“否”,我们将导航回之前选择的选项卡:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    NSInteger curTabIDX = self.selectedIndex;
    
    if ([viewController isKindOfClass:NeedsConsentViewController.class]) {

        NeedsConsentViewController *vc = (NeedsConsentViewController *)viewController;

        // whatever you're using to track the user's consent
        if (vc.hasConsent) {
            // allow the tab to be selected
            return YES;
        }

        // configure / instantiate your "Consent" view controller
        UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:@"Yes/No"
                                     message:@"Need your consent..."
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* okButton = [UIAlertAction
                                   actionWithTitle:@"Yes"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
            
            // however you're setting your user consent tracking
            vc.hasConsent = YES;
            
            // we've already navigated to the tab, with the Consent VC presented on top of it
            //  so nothing else to do
            
        }];
        
        [alert addAction:okButton];
        
        UIAlertAction* noButton = [UIAlertAction
                                   actionWithTitle:@"No"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action) {
            
            // user said NO, so return to the previous tab
            [self setSelectedIndex:curTabIDX];
            
        }];
        
        [alert addAction:noButton];
        
        [self presentViewController:alert animated:YES completion:nil];

        // show the tab behind the Consent VC
        return YES;
    }
    
    // all other tabs
    return YES;
}

注意:这只是 示例代码,无意成为,也不应该考虑,“生产就绪。”