[英]Is there any delegate that serves the purpose of tabBar willSelectItem?
I need to do some UI tasks when user selects a UI tab item.当用户选择 UI 选项卡项时,我需要执行一些 UI 任务。 Following delegate is available,
以下代表可用,
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Although, to answer for my particular question the internal UI transition issue is not important here, I am still sharing a overview code snippet.虽然,为了回答我的特定问题,内部 UI 转换问题在这里并不重要,但我仍然分享一个概述代码片段。
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[delegate.rootTabBarController showConsentErrorPage];
}
However, my UI task inside this delegate shows a glitch on the transition as it does the work after the tab is already shown.但是,我在此委托中的 UI 任务在转换时显示了一个小故障,因为它在选项卡已经显示后才开始工作。 I want to perform the UI task first before the UI being visible.
我想在 UI 可见之前先执行 UI 任务。 Any such delegate of trick to resolve this issue?
有没有这样的技巧代表来解决这个问题?
This may help you (without additional information, I can't really say).这可能会对您有所帮助(没有其他信息,我真的不能说)。
UITabBarController
conforming to <UITabBarControllerDelegate>
UITabBarController
符合<UITabBarControllerDelegate>
UITabBarController
, be sure to set self.delegate = self;
UITabBarController
中,一定要设置self.delegate = self;
in viewDidLoad
viewDidLoad
shouldSelectViewController
shouldSelectViewController
YES
(ie allow the tab to be selected)YES
(即允许选择选项卡)NO
NO
Here is some sample code...这是一些示例代码...
With this option, we present the "Ask Consent" controller, and only navigate to the "needs consent to see" tab when the user selects "Yes":使用此选项,我们会显示“请求同意”controller,并且仅在用户选择“是”时导航到“需要同意才能查看”选项卡:
- (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;
}
With this option, we present the "Ask Consent" controller and navigate to the "needs consent to see" tab behind it.使用此选项,我们会显示“征求同意”controller并导航到其后面的“需要同意才能查看”选项卡。 If the user answers "No" we navigate back to the previously selected tab:
如果用户回答“否”,我们将导航回之前选择的选项卡:
- (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;
}
Note: This is Example Code Only and is not intended to be, nor should be considered, "production ready."注意:这只是示例代码,不打算也不应被视为“生产就绪”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.