点击远程通知后导航栏丢失

时间:2019-09-02 00:54:40

标签: swift remote-notifications

我在应用程序中有一个侧面菜单,因此在单击通知时我导航以查看控制器,但由于未显示导航栏而无法导航至其他VC。

我已经在App Delegate中完成了此任务,并且在故事板中也有一个导航控制器。

let sb = UIStoryboard(name: "Main", bundle: nil)
        let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
        window?.rootViewController = otherVC;

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler:@escaping (UIBackgroundFetchResult) -> Void) {

    let state = application.applicationState
    if state == .inactive || state == .background {

        let sb = UIStoryboard(name: "Main", bundle: nil)
        let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
        window?.rootViewController = otherVC

        print("background")
    } else {
        print("foreground")

    }
}

1 个答案:

答案 0 :(得分:2)

当然,导航栏已删除;视图控制器需要嵌入在导航控制器中。

尝试一下:

application/json

注意:这将以您的视图控制器为根,提供全新的导航。从应用的初始屏幕手动导航时,您将无法“导航”回通常位于其之前的任何其他屏幕。

如果您需要在导航层次结构中重新创建一个特定的位置,则需要实例化所有以前的视图控制器直到要显示的位置,并将它们立即设置为导航控件的内容。导航堆栈。

虚拟示例:

let sb = UIStoryboard(name: "Main", bundle: nil)

let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
// (actually, you don't need to cast to MessageViewController here; 
// the returned reference already is guaranteed to at least be a 
// UIViewController instance; you don't need more specificity in this    
// case --i.e, embed in navigation)

let navigation = UINavigationController(rootViewController: otherVC)
window?.rootViewController = navigation

(此示例假定每个VC都位于单独的情节提要中,但如果适合您的设置,也可以使用let first = UIStoryboard(name: "First", bundle: nil).instantiateInitialViewController() let second = UIStoryboard(name: "Second", bundle: nil).instantiateInitialViewController() let top = UIStoryboard(name: "Mail", bundle: nil).instantiateInitialViewController() let vcs = [first, second, top] let navigation = UINavigationController() navigation.setViewController(vcs, animated: false) window?.rootViewController = navigation

现在,您可以使用导航控制器的后退按钮返回“第二”和“第一”。