尝试通过应用程序图标上的 3d 触摸从 performActionFor shortcutItem 将 VC 嵌入到 NavigationVC
Trying to Embed VC into NavigationVC from performActionFor shortcutItem via 3d touch on App icon
我有一个应用程序使用 3d touch 跳转到我应用程序中的某个 VC。问题是当应用程序正常启动时,我的所有 VC 都嵌入到导航视图控制器中。但是由于我跳过启动序列并直接跳转到 VC,所以我跳转到的 VC 没有嵌入导航 VC.
这是我在 App Delegate 中尝试的内容
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 嵌入到情节提要中设置的主导航 VC 中。 (嵌入式导航控制器是在情节提要中设置的,以防万一。)
选项 1:将 Storyboard Identifier
添加到您的 UINavigationController
,然后实例化 UINavigationController
。
选项 2:从故事板中删除 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()
我有一个应用程序使用 3d touch 跳转到我应用程序中的某个 VC。问题是当应用程序正常启动时,我的所有 VC 都嵌入到导航视图控制器中。但是由于我跳过启动序列并直接跳转到 VC,所以我跳转到的 VC 没有嵌入导航 VC.
这是我在 App Delegate 中尝试的内容
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 嵌入到情节提要中设置的主导航 VC 中。 (嵌入式导航控制器是在情节提要中设置的,以防万一。)
选项 1:将 Storyboard Identifier
添加到您的 UINavigationController
,然后实例化 UINavigationController
。
选项 2:从故事板中删除 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()