【问题标题】:Push to ViewController from AppDelegate with stack and navigation Bar使用堆栈和导航栏从 AppDelegate 推送到 ViewController
【发布时间】:2019-08-01 14:54:57
【问题描述】:

我正在开发一个消息应用程序,但在实现推送通知行为时遇到了问题。

我想要的是,当用户点击通知时,该组的对话会打开。但是必须有一个导航栏才能返回联系人列表。

我已经知道如何从通知中提取信息以获取联系人姓名。我正在 AppDelegate 上的函数 didReceive 中尝试这个。

到目前为止,我尝试过的是:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyandVisible()

这会打开正确的对话并显示消息,但 UI 元素没有响应(点击文本字段时无法写入,点击按钮时无法导航到图库等),并且没有导航栏。

所以在做了一些研究之后,我尝试了这个:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
let rootViewController = self.window?rootViewController as! UINavigationController
rootViewController. pushViewController(vc, animated: true)

这不起作用。没有错误消息,但屏幕只是白色。

这是我的故事板: 我将 SWRevealViewController 用于侧边菜单,消息传递部分有一个 tabBarController,联系人列表位于 2º 选项卡上。

编辑:使用第二种方法,有一个错误。

无法将 'SWRevealViewController 类型的值转换为 UINavigationController'

【问题讨论】:

    标签: swift uiviewcontroller push-notification appdelegate


    【解决方案1】:

    我认为您应该使用 NotificationCenter 来执行此操作。

    当用户点击通知时,您应该在 didReceive 中发布一个 NotificationCenter,如下所示:

    NotificationCenter.default.post(name: .onMessageRecieved, object: notificationObject)
    

    根据您所在的视图控制器,您应该在 viewDidLoad() 函数中添加一个观察者,以便它了解已收到如下消息:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.onMessageRecieved(notification:)), name: .onMessageRecieved, object: nil)
    
    }
    

    你应该在视图控制器中有一个 func (onMessageRecieved) 来进行这样的导航:

        func onMessageRecieved(notification: NSNotification){
    
                // open the conversation
    
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
    
                vc.contactName = name as String 
    
                self.navigationController?.pushViewController(vc, animated: true)
    
       }
    

    这样您就可以从视图控制器进行导航,因此导航也将在那里。

    【讨论】:

    • 感谢您的帮助。我在主要的ViewControllers 上添加了观察者,它可以工作。此方法的唯一缺点是在应用程序关闭时它不起作用。当我点击通知时,主页会打开。
    • 我认为你应该实现一个 UNNotificationServiceExtension 来更好地处理通知。您可以在那里发布事件而不是 AppDelegate
    【解决方案2】:

    无法将 'SWRevealViewController 类型的值转换为 UINavigationController'

    这个问题是因为“SWRevealViewController”不是 UINavigationController 的子类。所以你不能这样投射。

    要解决聊天 ViewController 的问题,你应该像这样修改你的代码。

    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
    let nav = vc.navigationController
    vc.contactName = name as String
    let rootViewController = self.window?rootViewController as! UIViewController
    rootViewController.presentViewController(nav, animated: true, completion: nil)
    

    但聊天 VC 将处于演示者模式。你应该处理解雇。

    你只要试试这个,让我知道它是否有效。

    【讨论】:

    • 感谢您的回答。不幸的是它不起作用,它崩溃了,因为nav 为零。
    • 但我可以看到 UINavigationControler 嵌入在 ChatViewController 上。您可以尝试获取 nil nav 对象的 UINavigationControler 引用吗?一旦你能找到它,问题就会得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多