當用戶選擇 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 任務。有沒有這樣的技巧代表來解決這個問題?
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;
}
注意:這只是示例代碼,不打算也不應被視為“生產就緒”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/406553.html
標籤:
上一篇:iOS-使用協議和委托將資料從Swift傳遞到ObjCVC
下一篇:訓練神經網路來學習多項式方程
