尝试使用 localnotification 呈现其视图不在窗口层次结构中的 UIAlertController

编程入门 行业动态 更新时间:2024-10-27 05:32:57
本文介绍了尝试使用 localnotification 呈现其视图不在窗口层次结构中的 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在用户单击本地通知后显示 AlertView.AlertView 有取消或确定选项.

I am trying to present AlertView once the user clicks on the local notification. The AlertView has options of cancel or ok.

extension ViewController:UNUserNotificationCenterDelegate{ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("Tapped in notification") if(defaults.object(forKey: "alertOn") != nil){ // Create the alert controller let alertController = UIAlertController(title: "Some text", message: "Some text again", preferredStyle: .alert) // Create the actions let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { UIAlertAction in } let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { UIAlertAction in } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.present(alertController, animated: true, completion: nil) } } func triggerNotification(){ print("notification will be triggered in five seconds..Hold on tight") let content = UNMutableNotificationContent() content.title = "SomeText" content.body = "Some more text" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false) let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger) UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate UNUserNotificationCenter.current().add(request){(error) in if (error != nil){ print(error?.localizedDescription) } } }

它应该向我显示带有确定或取消请求选项的警报视图.相反,它向我显示消息 UIAlertController 其视图不在窗口层次结构中

It should show me the alertview with option to ok or cancel a request. Instead its showing me message UIAlertController whose view is not in the window hierarchy

当我将 alertview 放在 viewdidapear 中时,它工作正常,但是当我将它放在 userNotificationCenter 中时,我的 AlertView 没有附加到主视图中.

When I put the alertview in viewdidapear it works fine but when I put it in userNotificationCenter my AlertView not get attached to the main view.

代码简图

extension ViewController:UNUserNotificationCenterDelegate{ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { Present UIAlertView } } ViewController{ Call to notification when app is in background triggerNotification() triggerNotification(){ Definition } }

推荐答案

试试这个

func currentTopViewController() -> UIViewController { var topVC: UIViewController? = UIApplication.shared.delegate?.window?.rootViewController while topVC?.presentedViewController { topVC = topVC?.presentedViewController } return topVC! }

并将VC呈现为

let currentTopVC: UIViewController? = self.currentTopViewController() currentTopVC.present(alertController, animated: true, completion: nil)

更多推荐

尝试使用 localnotification 呈现其视图不在窗口层次结构中的 UIAlertController

本文发布于:2023-11-27 19:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   层次   窗口   结构   localnotification

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!