UIAlertController显示了UIAlertActions的空白标题

3
最近,我终于重构了一个旧项目,并将所有的UIAlertViews和UIActionSheets转换为UIAlertControllers。但是,我开始收到客户反馈说有时UIAlertController的操作标题是空白的,就像这样:blank actions titles example。我无法在我的设备上复现这个错误,你有任何关于可能原因的想法吗?
更新:我认为问题出现在这段代码中,可能主窗口的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

你能展示一些代码作为例子吗?说明一下你尝试过的内容! - Bhupat Bheda
请返回翻译文本。 - V-Dev
2个回答

2
最终,我找到了解决方案。经过一些实验,我意识到为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;
}

我所做的是移除了继承主窗口 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];

我的代码写法完全一样,我非常熟悉UIAlertController,但这个问题很奇怪,我仍然无法弄清楚。 - avsmirnov567
这是不可能的,你遇到过这种问题吗?也许这是一个奇怪的问题。 - Kuldeep
@AleksandrSmirnov,请在你的问题中加入代码,这样我们才能查看。 - Kuldeep
我已经更新了问题,你能否请查看代码,并确认我的正确性? - avsmirnov567
让我们在聊天中继续这个讨论 - avsmirnov567
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接