2017-10-18 53 views
2

最近,我終於重構了一個傳統項目,並已將所有UIAlertViewsUIActionSheets轉換爲UIAlertControllers。但我已經開始接受客戶的問題,他們說有時UIAlertController操作標題是空白的。像這樣:UIAlertController顯示空白標題的UIAlertActions

blank actions titles example

我不能重現這個bug在我的設備,你有沒有對可能的原因,任何想法?

UPDATE:

我相信,這個問題被覆蓋的代碼,也許主窗口莫名其妙不是默認的tintColor,並UIAlertController繼承呢?

@implementation UIAlertController (WMC) 

    - (void)show:(BOOL)animated { 
     self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen 
    mainScreen].bounds]; 
     self.alertWindow.rootViewController = [[UIViewController alloc] init]; 

     id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate; 
     // Applications that does not load with UIMainStoryboardFile might not have a window property: 
     if ([delegate respondsToSelector:@selector(window)]) { 
      // we inherit the main window's tintColor 
      self.alertWindow.tintColor = delegate.window.tintColor; 
     } 

     // window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard) 
     UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject; 
     self.alertWindow.windowLevel = topWindow.windowLevel + 1; 

     [self.alertWindow makeKeyAndVisible]; 
     [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil]; 
    } 

    @end 
+0

你可以展示一些代碼,例如你試用過的東西! –

+0

例如:http://hayageek.com/uialertcontroller-example-ios/ –

回答

1

最後,我已經找到了解決辦法。經過一些實驗後,我意識到,設置tintColor財產爲UIWindow結果在所有UIAlertActions改變tintColor在UIWindow呈現。所以,我已經改變了UIAlertController擴展的代碼(注意:原始代碼不是我的,我在SoF上發現它,這要感謝它的作者),我用這種方式顯示我的提醒:

#import "UIAlertController+WMC.h" 

@implementation UIAlertController (Private) 

@dynamic alertWindow; 

- (void)setAlertWindow:(UIWindow *)alertWindow { 
    objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

- (UIWindow *)alertWindow { 
    return objc_getAssociatedObject(self, @selector(alertWindow)); 
} 

@end 

@implementation UIAlertController (WMC) 

- (void)show { 
    [self show:YES]; 
} 

- (void)show:(BOOL)animated { 
    self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    self.alertWindow.rootViewController = [[UIViewController alloc] init]; 

    // window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard) 
    UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject; 
    self.alertWindow.windowLevel = topWindow.windowLevel + 1; 

    [self.alertWindow makeKeyAndVisible]; 
    [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    // precaution to insure window gets destroyed 
    self.alertWindow.hidden = YES; 
    self.alertWindow = nil; 
} 

我剛剛做的是刪除了繼承主窗口tintColor的部分。呈現警報的這種複雜方式是我的項目中導致架構不良的結果。感謝所有評論員對這個問題感興趣。

1

試試這個代碼

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Title Message" preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ 
     NSLog(@"Cancle Tapped"); 
     [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 

    [alertController addAction:cancelAction]; 

    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", @"Yes action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     NSLog(@"Yes Tapped"); 
    }]; 
    [alertController addAction:yesAction]; 

    UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", @"No action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
     NSLog(@"No Tapped"); 
    }]; 
    [alertController addAction:noAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
+0

我的代碼寫的方式完全一樣,我對UIAlertController非常熟悉,但這個問題很奇怪,我仍然無法弄清楚 –

+0

@AleksandrSmirnov,你能告訴我,你的客戶在哪個設備上獲得空白的標題? – Kuldeep

+0

幾乎在每個模型@kuldeep –