Ios UIAlertController未从AppDelegate显示

Ios UIAlertController未从AppDelegate显示,ios,objective-c,cocoa-touch,uialertcontroller,Ios,Objective C,Cocoa Touch,Uialertcontroller,我试图全局(从不是视图控制器的位置)显示UIAlertController,但我的警报没有显示。我正在测试我的初始警报,该警报在检测到无网络后通过应用程序委托中的可达性测试显示。我可以在屏幕上看到发射图像,但警报未显示。如果我使用UIAlertView,一切看起来都很好 如果您遇到过类似问题,请告知 UIAlertController *alert = [UIAlertController alertControllerWithTitle:iTitle message:iMessage pref

我试图全局(从不是视图控制器的位置)显示
UIAlertController
,但我的警报没有显示。我正在测试我的初始警报,该警报在检测到无网络后通过应用程序委托中的可达性测试显示。我可以在屏幕上看到发射图像,但警报未显示。如果我使用
UIAlertView
,一切看起来都很好

如果您遇到过类似问题,请告知

UIAlertController *alert = [UIAlertController alertControllerWithTitle:iTitle message:iMessage preferredStyle:UIAlertControllerStyleAlert];
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

if ([rootViewController isKindOfClass:[UINavigationController class]]) {
   rootViewController = [((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}

dispatch_async(dispatch_get_main_queue(), ^{
    [rootViewController presentViewController:alert animated:YES completion:^{
        [aBlockSelf alertPresented];
    }];
});
编辑:

为什么我要从
AppDelegate
显示它?

正如我所说,在执行可达性测试时,我会向用户显示一个警报。即使在加载任何视图控制器之前,也必须从
AppDelegate
中执行此测试。因此,我在我的
ui窗口上显示了一个启动屏幕,我希望我的警报出现在它上面

当我显示警报时,我的窗口设置是否正确?

是的,我正在确保正确设置我当前的警报控制器post窗口,并且在执行以下行之后

[self.window addSubview:self.rootViewController.view];
[self.window makeKeyAndVisible];

设置Rootviewcontroller而不是添加子视图

   [self.window setRootViewController: self.rootViewController];

设置Rootviewcontroller而不是添加子视图

   [self.window setRootViewController: self.rootViewController];

从视图控制器发送包含当前视图控制器的通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAlertNotificationKey"
                                                        object:self
                                                      userInfo:nil];
- (void)showAlert:(NSNotification *)notification {
    UIViewController *viewController = notification.object;
    // yourAlertControler..
    [viewController presentViewController:yourAlertController animated:YES completion:nil];
}
在AppDelegate中添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(showAlert:)
                                             name:@"ShowAlertNotificationKey"
                                           object:nil];
以及显示AlertController的方法:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAlertNotificationKey"
                                                        object:self
                                                      userInfo:nil];
- (void)showAlert:(NSNotification *)notification {
    UIViewController *viewController = notification.object;
    // yourAlertControler..
    [viewController presentViewController:yourAlertController animated:YES completion:nil];
}

从视图控制器发送包含当前视图控制器的通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAlertNotificationKey"
                                                        object:self
                                                      userInfo:nil];
- (void)showAlert:(NSNotification *)notification {
    UIViewController *viewController = notification.object;
    // yourAlertControler..
    [viewController presentViewController:yourAlertController animated:YES completion:nil];
}
在AppDelegate中添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(showAlert:)
                                             name:@"ShowAlertNotificationKey"
                                           object:nil];
以及显示AlertController的方法:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAlertNotificationKey"
                                                        object:self
                                                      userInfo:nil];
- (void)showAlert:(NSNotification *)notification {
    UIViewController *viewController = notification.object;
    // yourAlertControler..
    [viewController presentViewController:yourAlertController animated:YES completion:nil];
}

好的,我通过创建一个带有空视图控制器的
UIWindow
对象并在其中显示我的警报,解决了这个问题。显然,一旦警报控制器被解除,这个临时窗口也会消失

self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:^{
    [aBlockSelf alertPresented];
}];

感谢WWDC实验室课程的引用:-)

好的,我通过创建一个带有空视图控制器的
UIWindow
对象并在其中显示我的警报,解决了这个问题。显然,一旦警报控制器被解除,这个临时窗口也会消失

self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:^{
    [aBlockSelf alertPresented];
}];

感谢WWDC实验室课程的引用:-)

对于swift 3.0,这将起作用-

let topWindow = UIWindow(frame: UIScreen.main.bounds)  
topWindow.rootViewController = UIViewController()             
topWindow.windowLevel = UIWindowLevelAlert + 1

let alert = UIAlertController(title: "No Network Connection", message:   "Please check your connection and try again.", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in

     topWindow.isHidden = true
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

对于swift 3.0,这将起作用-

let topWindow = UIWindow(frame: UIScreen.main.bounds)  
topWindow.rootViewController = UIViewController()             
topWindow.windowLevel = UIWindowLevelAlert + 1

let alert = UIAlertController(title: "No Network Connection", message:   "Please check your connection and try again.", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in

     topWindow.isHidden = true
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

为什么您试图从应用程序委托执行此操作?代码似乎很好…为什么您试图从应用程序委托执行此操作?代码似乎很好…确保在设置窗口根视图控制器后显示alertcontroller确保在设置窗口根视图控制器后显示alertcontroller您是英雄:)您是英雄:)