gpt4 book ai didi

ios - 如何从 appDelegate 呈现 UIAlertView

转载 作者:IT王子 更新时间:2023-10-29 05:22:11 24 4
gpt4 key购买 nike

我正在尝试从 appDelegate 中显示一个 UIAlertView didReceiveRemoteNotification当应用收到推送通知时。

我有这个错误:

  Warning: Attempt to present <UIAlertController: 0x14c5494c0> on <UINavigationController:
0x14c60ce00> whose view is not in the window hierarchy!

这是我的代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {

var contentPush: NSDictionary = userInfo.objectForKey("aps") as NSDictionary

var message = contentPush.objectForKey("alert") as String



let alertController = UIAlertController(title: "Default Style", message: message, preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

let photoPushedVc = self.storyboard.instantiateViewControllerWithIdentifier("CommentTableViewController") as CommentTableViewController

println("the fetched post is \(post)")

photoPushedVc.post = post

let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController

activeVc?.presentViewController(photoPushedVc, animated: true, completion: nil)
}

alertController.addAction(OKAction)

let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController


activeVc?.presentViewController(alertController, animated: true, completion: nil)}

最佳答案

要使用 Objective-CAppDelegate 生成 AlertController 对话框,

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Hello World!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];

类型 1

UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

类型 2

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];

两者都经过了测试并且工作正常。

关于ios - 如何从 appDelegate 呈现 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27807104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com