UIContentView中的当前视图控制器

时间:2017-05-25 11:16:21

标签: ios objective-c presentviewcontroller

我在rootViewController上有pageViewController(ABC),pageViewController包含ViewController(PQR),其中包含ContentView。我想呈现ViewController,但它正在说

  

警告:尝试在ABC上显示XYZ:0x17ee0730:0x17e67dc0,其视图不在窗口层次结构中!

我想在哪个控制器上显示View控制器

1 个答案:

答案 0 :(得分:0)

很高兴你的问题得到了解决。如果你想从视图控制器以外的任何UIView显示警报,这将有所帮助。

此方法获取顶视图控制器,以便您可以从中显示警报:

func currentTopViewController() -> UIViewController{

    var topVC: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController

    while ((topVC?.presentedViewController) != nil) {

        topVC = topVC?.presentedViewController

    }

    return topVC!

}

func showAlert() { // This presents your alert from the received view controller object

    let alertController: UIAlertController = UIAlertController.init(title: "title", message: "message", preferredStyle: .alert)

    let alertAction = UIAlertAction.init(title: "OK", style: .default) { (UIAlertAction) in
            //Your action    
    }

    alertController.addAction(alertAction)

    currentTopViewController().present(alertController, animated: true, completion: nil)

}