拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 是否有任何代表用于tabBarwillSelectItem的目的?

是否有任何代表用于tabBarwillSelectItem的目的?

白鹭 - 2022-03-09 2094 0 0

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

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

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

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

uj5u.com热心网友回复:

可能会对您有所帮助(没有其他信息,我真的不能说)。

  • 子类UITabBarController符合<UITabBarControllerDelegate>
  • 在新的自定义UITabBarController ,一定要设定self.delegate = self;viewDidLoad
  • 实施 shouldSelectViewController
  • 如果该控制器是您的“需要同意查看”视图控制器,
    • 检查用户是否已经同意
    • 如果是,则回传YES(即允许选择选项卡)
    • 如果没有,请出示您的“征求同意”控制器并回传 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;
}

注意:这只是示例代码,不打算也不应被视为“生产就绪”。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *