警告:尝试在其 ViewController 上呈现 ViewController 不在视图层次结构中(3D Touch)
Warning: Attempt to Present ViewController on whose ViewController isn't in the view hierarchy (3D Touch)
基本上,我正在尝试在我的应用程序中实现主屏幕快速操作。当我点击其中一项快速操作时,出现错误:
Warning: Attempt to present theViewController on ViewController whose view is not in the window hierarchy!
我查看了其他一些具有相同问题的 Stack Over Flow 帖子,但解决方案对我不起作用。此外,在我的 applicationDidEnterBackground
方法中,我添加了 self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil).
以下是我在 App Delegate 中包含的一些相关 3D Touch 方法:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let handledShortcutItem = self.handleShortuctItem(shortcutItem)
completionHandler(handledShortcutItem)
}
此外,我还有这些辅助方法:
enum ShortcutIdentifier: String {
case First
case Second
init?(fullType: String) {
guard let last = fullType.componentsSeparatedByString(".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
}
}
func handleShortuctItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
var handled = false
guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false }
guard let shortcutType = shortcutItem.type as String? else { return false }
switch(shortcutType) {
case ShortcutIdentifier.First.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as! FirstViewController
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
break
case ShortcutIdentifier.Second.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
break
default:
break
}
return handled
}
一种无意识的解决方案是在您的 presentViewController
调用周围设置延迟(delay
定义为:dispatch_after - GCD in swift?):
switch(shortcutType) {
case ShortcutIdentifier.First.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as! FirstViewController
delay(0.3) {
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
}
break
case ShortcutIdentifier.Second.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
delay(0.3) {
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
}
break
default:
break
}
想法是让界面有时间在尝试进行演示之前完成显示。
基本上,我正在尝试在我的应用程序中实现主屏幕快速操作。当我点击其中一项快速操作时,出现错误:
Warning: Attempt to present theViewController on ViewController whose view is not in the window hierarchy!
我查看了其他一些具有相同问题的 Stack Over Flow 帖子,但解决方案对我不起作用。此外,在我的 applicationDidEnterBackground
方法中,我添加了 self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil).
以下是我在 App Delegate 中包含的一些相关 3D Touch 方法:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let handledShortcutItem = self.handleShortuctItem(shortcutItem)
completionHandler(handledShortcutItem)
}
此外,我还有这些辅助方法:
enum ShortcutIdentifier: String {
case First
case Second
init?(fullType: String) {
guard let last = fullType.componentsSeparatedByString(".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
}
}
func handleShortuctItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
var handled = false
guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false }
guard let shortcutType = shortcutItem.type as String? else { return false }
switch(shortcutType) {
case ShortcutIdentifier.First.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as! FirstViewController
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
break
case ShortcutIdentifier.Second.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
break
default:
break
}
return handled
}
一种无意识的解决方案是在您的 presentViewController
调用周围设置延迟(delay
定义为:dispatch_after - GCD in swift?):
switch(shortcutType) {
case ShortcutIdentifier.First.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewController") as! FirstViewController
delay(0.3) {
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
}
break
case ShortcutIdentifier.Second.type:
handled = true
let navVC = mainStoryboard.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
delay(0.3) {
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
}
break
default:
break
}
想法是让界面有时间在尝试进行演示之前完成显示。