UIAlertController 显示 UIAlertActions 的空白标题

UIAlertController shows blank titles of UIAlertActions

最近,我终于重构了一个遗留项目,并将所有 UIAlertViewsUIActionSheets 转换为 UIAlertControllers。但我已经开始收到客户的问题,他们说有时 UIAlertController 操作标题是空白的。像那样:

我无法在我的设备上重现此错误,您对可能的原因有任何想法吗?

更新:

我相信,该问题已包含在该代码中,也许 main window 的 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

试试这个代码

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];

终于,我找到了解决方案。经过一些实验,我意识到,为 UIWindow 设置 tintColor 属性 会导致更改 UIWindow 中显示的所有 UIAlertActions 的 tintColor。所以,我更改了 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;
}

我所做的就是删除继承main window tintColor 的部分。如此复杂的警报呈现方式是我项目中导航架构不佳的结果。感谢所有对此问题感兴趣的评论员。