UIAlertController显示空白标题的UIAlertActions

问题描述:

最近,我终于重构了一个传统项目,并已将所有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/ –

最后,我已经找到了解决办法。经过一些实验后,我意识到,设置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的部分。呈现警报的这种复杂方式是我的项目中导致架构不良的结果。感谢所有评论员对这个问题感兴趣。

试试这个代码

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 –