场景:用户点击视图控制器上的按钮。视图控制器是导航堆栈中的最顶层(显然)。tap调用另一个类上调用的实用程序类方法。在那里发生了一件坏事,我想在控件返回到视图控制器之前在那里显示一个警告。
+ (void)myUtilityMethod {
// do stuff
// something bad happened, display an alert.
}这在UIAlertView中是可能的(但可能不太合适)。
在本例中,您如何表示UIAlertController,就在myUtilityMethod中
发布于 2015-05-28 13:39:30
几个月前,我发布了一个相似问题,认为我终于解决了这个问题。如果您只想查看代码,请按照我文章底部的链接进行操作。
解决方案是使用额外的UIWindow。
当您想显示您的UIAlertController时:
window.makeKeyAndVisible())window.rootViewController = UIViewController())有几件事要注意:
window.windowLevel = UIWindowLevelAlert + 1)最后,我有一个完整的实现,如果您只想看看这个。
发布于 2015-06-19 15:00:09
在万维网,我在其中一个实验室停下来,问了一个苹果工程师同样的问题:“展示UIAlertController的最佳实践是什么?”他说,他们已经得到了这个问题很多,我们开玩笑说,他们应该有一个会议就它。他说,苹果内部正在创建一个具有透明UIViewController的UIViewController,然后在其上展示UIAlertController。基本上迪伦·贝特曼的答案是什么。
但是我不想使用UIAlertController的子类,因为这需要我在整个应用程序中修改我的代码。因此,在关联对象的帮助下,我在UIAlertController上创建了一个类别,它在object中提供了一个show方法。
以下是相关代码:
#import "UIAlertController+Window.h"
#import <objc/runtime.h>
@interface UIAlertController (Window)
- (void)show;
- (void)show:(BOOL)animated;
@end
@interface UIAlertController (Private)
@property (nonatomic, strong) UIWindow *alertWindow;
@end
@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 (Window)
- (void)show {
[self show:YES];
}
- (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];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// precaution to ensure window gets destroyed
self.alertWindow.hidden = YES;
self.alertWindow = nil;
}
@end下面是一个示例用法:
// need local variable for TextField to prevent retain cycle of Alert otherwise UIWindow
// would not disappear after the Alert was dismissed
__block UITextField *localTextField;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Global Alert" message:@"Enter some text" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"do something with text:%@", localTextField.text);
// do NOT use alert.textfields or otherwise reference the alert in the block. Will cause retain cycle
}]];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
localTextField = textField;
}];
[alert show];当释放UIWindow时,创建的UIAlertController将被销毁,因为它是唯一保留UIWindow的对象。但是,如果您将UIAlertController分配给一个属性,或者通过访问其中一个操作块中的警报使其保留计数增加,则UIWindow将留在屏幕上,锁定您的UI。在需要访问UITextField的情况下,请参阅上面的示例使用代码以避免。
我用一个测试项目做了GitHub回购:FFGlobalAlertController
发布于 2014-11-04 13:10:25
Swift
let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
//...
var rootViewController = UIApplication.shared.keyWindow?.rootViewController
if let navigationController = rootViewController as? UINavigationController {
rootViewController = navigationController.viewControllers.first
}
if let tabBarController = rootViewController as? UITabBarController {
rootViewController = tabBarController.selectedViewController
}
//...
rootViewController?.present(alertController, animated: true, completion: nil)目标-C
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//...
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
}
//...
[rootViewController presentViewController:alertController animated:YES completion:nil];https://stackoverflow.com/questions/26554894
复制相似问题