Ios 3d touch swift 3不会打开viewcontroller
我在我的应用程序代理中为shortcutitems提供了一个3d touch功能,当3d touch项从iphone上单击时,它必须实例化一个viewcontroller 代码如下:Ios 3d touch swift 3不会打开viewcontroller,ios,swift,xcode,appdelegate,3dtouch,Ios,Swift,Xcode,Appdelegate,3dtouch,我在我的应用程序代理中为shortcutitems提供了一个3d touch功能,当3d touch项从iphone上单击时,它必须实例化一个viewcontroller 代码如下: func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(exerciseVC, animated: false, completion: { () -> Void in
completionHandler(true)
})
}
}
此代码导致以下警告/错误:
Warning: Attempt to present <ActiveRest.exerciseVC: 0x131d39320> on <ActiveRest.ViewController: 0x131d0e470> whose view is not in the window hierarchy!
警告:尝试显示其视图不在窗口层次结构中的对象!
这和耽搁有关吗
谢谢你的帮助
问候。
视图不在窗口层次结构中
,这意味着您的viewcontroller的视图仍没有加载到内存中。因此,您无法在其上显示viewcontroller
。您应该必须从viewdide显示viewcontroller的viewcontroller(我的意思是从您的rootviewController)
我做了此更改,一切正常。但这是在应用程序委派中调用的。您不能从appdelegate
显示viewcontroller,因为您的viewHierarchy
在appdelegate
中未准备就绪!我观看了本教程,并做了完全相同的操作:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
window?.rootViewController?.addChildViewController(exerciseVC)
}
}