swift - 警告 : Attempt to Present ViewController on whose ViewController isn't in the view hierarchy (3D Touch)

标签 swift appdelegate 3dtouch

<分区>

基本上,我正在尝试在我的应用中实现主屏幕快速操作。当我点击其中一个快速操作时,出现错误:

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
}

想法是让界面有时间在尝试进行演示之前完成显示。

关于swift - 警告 : Attempt to Present ViewController on whose ViewController isn't in the view hierarchy (3D Touch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822931/

相关文章:

swift - Xcode 9.1 升级到 Swift 4 中断#ifdef

ios - 在 swift 3 中预览操作项

ios - xcode:类不符合键值编码

swift - 为什么timer失效后还继续执行?

ios - 运行指定的 ViewController

core-data - 使用 SceneDelegate 设置 CoreData - 未知标识符 'window' 错误 - iOS 13 及更高版本

ios - 当用户终止应用程序时,我可以进行 api 调用吗?

ios - 使用 Swift 3D 触摸力值改变声音的音量

ios - 何时以及多久填充一次动态快捷方式 (UIApplication.sharedApplication.shortcutItems)?

ios - AVPlayerController : Full screen Autolayout issues