来自超类的Swift presentViewController不起作用

时间:2015-11-03 14:32:09

标签: ios swift inheritance

我有一个超类,它有一个应该在子类上呈现视图控制器的函数。我有两个来自超类的子类,它应该出现在当前可见的任何一个上。目前它只出现在其中一个并且只有在可见时才出现。

骨架代码:

class Superclass : UIViewController {
    func somethingHappened() {
        if let navigation = self.navigationController {
            navigation.presentViewController(controllerToPresent, animated: true, completion: { () -> Void in
                // Stuff
            })
        } else {
            self.presentViewController(controllerToPresent, animated: true, completion: { () -> Void in
                // Stuff
            })
        }
    }
}

我的一个视图控制器SubclassA是导航控制器的根视图控制器,它通过函数的else。另一个SubclassB被推过SubclassA,这将通过第一部分。 SubclassB是唯一成功显示视图控制器的视图控制器。

controllerToPresent也不是零。

Warning: Attempt to present <UINavigationController> on <SubclassB> whose view is not in the window hierarchy!

1 个答案:

答案 0 :(得分:0)

警告试图告诉您的是,您正在presentViewController:animated:completion:的实例上调用方法SubclassA,但该SubclassA实例的视图不在视图层次结构中

可能解决您的问题的方法是在呈现视图控制器之前将视图添加到视图层次结构中,例如,通过调用

view.addSubview(<InstanceOfSubclass>.view)