在 UIContentView 中呈现视图控制器
present view controller in UIContentView
我的页面ViewController(ABC) 位于根目录ViewController,页面ViewController 包含ViewController(PQR),其中包含ContentView。我想展示 ViewController 但它说的是
Warning: Attempt to present XYZ: 0x17ee0730 on ABC: 0x17e67dc0 whose view is not in the window hierarchy!
我想在哪个控制器上显示视图控制器
很高兴您的问题得到解决。如果您想从视图控制器以外的任何 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)
}
我的页面ViewController(ABC) 位于根目录ViewController,页面ViewController 包含ViewController(PQR),其中包含ContentView。我想展示 ViewController 但它说的是
Warning: Attempt to present XYZ: 0x17ee0730 on ABC: 0x17e67dc0 whose view is not in the window hierarchy!
我想在哪个控制器上显示视图控制器
很高兴您的问题得到解决。如果您想从视图控制器以外的任何 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)
}