簡體   English   中英

嘗試通過3D觸摸App圖標從performActionForshortcutItem將VC嵌入到NavigationVC中

[英]Trying to Embed VC into NavigationVC from performActionFor shortcutItem via 3d touch on App icon

我有一個使用3d touch跳轉到我的應用程序中某個VC的應用程序。 問題是,當應用正常啟動時,我所有的VC都嵌入了Navigation View Controller。 但是由於我跳過了啟動順序,而是直接跳到了VC中,所以我跳到的VC沒有嵌入到Nav VC中。

這是我在應用程序委托中嘗試的

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    guard TouchActions(rawValue: shortcutItem.type) != nil else {
        print("3d not working")
        completionHandler(false)
        return
    }

   print("3d touch workig")

    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let addPersonVC : AddPersonViewController = mainStoryboard.instantiateViewController(withIdentifier: "AddPerson") as! AddPersonViewController
    // pass the stack to the addPersonVC
    addPersonVC.coreDataStack = coreDataStack


    let navController:UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
    navController.pushViewController(addPersonVC, animated: true)


   // self.window? = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navController
    self.window?.makeKeyAndVisible()

    completionHandler(true)
}

這段代碼有效,但是當您嘗試退出此VC時,該應用程序只是停滯不前。 我介紹了如何將addPersonVC嵌入故事板中設置的主要Nav VC中。 (如果需要的話,嵌入式導航控制器是在情節提要中設置的。)

選項1:向您的UINavigationController添加一個Storyboard Identifier ,然后實例化UINavigationController

選項2:從情節提要中刪除UINavigationController 只需通過代碼初始化一個新的UINavigationController並以編程方式設置rootView

我不是Swift開發人員,但是由於您看不到我編寫的示例,因此我做了一些快速粘貼以幫助您了解基礎知識,請嘗試以下代碼:

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let addPersonVC : AddPersonViewController = mainStoryboard.instantiateViewController(withIdentifier: "AddPerson") as! AddPersonViewController
    // pass the stack to the addPersonVC
    addPersonVC.coreDataStack = coreDataStack

let navController:UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
    navController.viewControllers = [addPersonVC]


    self.window?.rootViewController?.present(navController, animated: true, completion: nil)
    self.window?.makeKeyAndVisible()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM