代码之家  ›  专栏  ›  技术社区  ›  Chhorn Soro

从AppDelegate调用navigationController

  •  0
  • Chhorn Soro  · 技术社区  · 7 年前

    现在我正在实现onesignal通知,当用户单击通知时,我想打开特定的Viewcontroller。

    这是我在AppDelegate的代码

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let detailBrand = storyBoard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
    detailBrand.getValue = value
    navigationController?.pushViewController(detailBrand, animated: true)
    

    如果我将此代码放在任何ViewController类中,但在AppDelegate中,它就不能正常工作。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mohmmad S    7 年前

    Appdelgate 不是一个 UIViewController ,

    所以你不能用 pushViewController(detailBrand, animated: true) 因为你现在的处境不太好 UIVewController 但是相反,你可以启动一个 UINavigatetionController 把它设为你的根,然后从根上推下去。

    你的代码应该是这样的

    let rootViewController = self.window!.rootViewController as! UINavigationController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
    rootViewController.pushViewController(profileViewController, animated: true)
    

    didFinishLaunchWithOption 方法。

    func application(_ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool