最近,我终于重构了一个遗留项目,并将所有UIAlertViews
和UIActionSheets
变为UIAlertControllers
。但我开始收到客户的问题,他们说有时候UIAlertController
行动标题是空白的。就像那样:
我无法在我的设备上重现此错误,您对可能的原因有任何想法吗?
更新
我相信,该代码中包含该问题,主窗口的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 :(得分:2)
最后,我找到了解决方案。经过一些实验,我意识到,为tintColor
设置UIWindow
属性会导致更改UIAlertActions
中显示的所有UIWindow
的tintColor。所以,我已经更改了UIAlertController
扩展的代码(注意:原始代码不是我的,我在SoF找到它,感谢它的作者),我用它以这种方式呈现我的警报: / p>
#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 :(得分: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];