尝试呈现其视图不在 window 层次结构中
Attempt to present whose view is not in the window hierarchy
我正在尝试在 swift
中创建警报控制器 class
//AppDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame:UIScreen.mainScreen().bounds)
let loginVC = ViewControllerForLogin (nibName:"ViewControllerForLogin", bundle:nil)
navigationObject = UINavigationController(rootViewController: loginVC)
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
return true
}
//SPSwiftAlert.swift
class SPSwiftAlert: UIViewController {
//#MARK: - Members
internal var defaultTextForNormalAlertButton = "OK"
static let sharedObject = SPSwiftAlert()
//#MARK: Functions
func showNormalAlert(controller: UIViewController, title: String, message: String) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil))
// show the alert
controller.presentViewController(alert, animated: true, completion: nil)
}
}
the above class is used display the alert with provided message and
title with single button as-
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
但这给了我运行时错误
Attempt to present <UIAlertController: 0x7f8c805e8e80> on <Swaft_Login_Demo.ViewControllerForLogin: 0x7f8c8042d4b0> whose view is not in the window hierarchy!
我该如何解决?
所以,当我看到你的代码时,我不明白你把 window.rootViewController 你的 loginVC 而不是导航的部分..
window?.rootViewController = navigationObject
然后,当您调用警报时,您似乎不在 window 的视图层次结构中。
尝试将此调用写入 viewDidAppear:
方法。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
dispatch_async(dispatch_get_main_queue()) {
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
}
}
NOTE: This generally happens when we try to show (present/push) the view
controller
over another view controller but the presenter view controller is
currently not active view controller (means the presenter view
controller view must be top view on the screen)
- 首先,为什么您不在视图控制器本身上显示警报控制器,而是创建一个新的视图控制器并在其中传递自我(您的viewcontroller)以显示警报。
- 就问题而言,您从未将 "SPSwiftAlert" 视图添加到任何视图。
// 添加一个动作(按钮)
alert.addAction(UIAlertAction(标题:defaultTextForNormalAlertButton,样式:UIAlertActionStyle.Default,处理程序:nil))
// 添加视图
// 添加 self.view 子视图到 controller.view
// 显示警报
self.presentViewController(警报,动画:真,完成:无)
我猜你的 ViewControllerForLogin 已经出现了,或者它在故事板中没有 segue。
我正在尝试在 swift
中创建警报控制器 class//AppDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame:UIScreen.mainScreen().bounds)
let loginVC = ViewControllerForLogin (nibName:"ViewControllerForLogin", bundle:nil)
navigationObject = UINavigationController(rootViewController: loginVC)
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
return true
}
//SPSwiftAlert.swift
class SPSwiftAlert: UIViewController {
//#MARK: - Members
internal var defaultTextForNormalAlertButton = "OK"
static let sharedObject = SPSwiftAlert()
//#MARK: Functions
func showNormalAlert(controller: UIViewController, title: String, message: String) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil))
// show the alert
controller.presentViewController(alert, animated: true, completion: nil)
}
}
the above class is used display the alert with provided message and title with single button as-
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
但这给了我运行时错误
Attempt to present <UIAlertController: 0x7f8c805e8e80> on <Swaft_Login_Demo.ViewControllerForLogin: 0x7f8c8042d4b0> whose view is not in the window hierarchy!
我该如何解决?
所以,当我看到你的代码时,我不明白你把 window.rootViewController 你的 loginVC 而不是导航的部分..
window?.rootViewController = navigationObject
然后,当您调用警报时,您似乎不在 window 的视图层次结构中。
尝试将此调用写入 viewDidAppear:
方法。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
dispatch_async(dispatch_get_main_queue()) {
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
}
}
NOTE: This generally happens when we try to show (present/push) the view controller over another view controller but the presenter view controller is currently not active view controller (means the presenter view controller view must be top view on the screen)
- 首先,为什么您不在视图控制器本身上显示警报控制器,而是创建一个新的视图控制器并在其中传递自我(您的viewcontroller)以显示警报。
- 就问题而言,您从未将 "SPSwiftAlert" 视图添加到任何视图。
// 添加一个动作(按钮) alert.addAction(UIAlertAction(标题:defaultTextForNormalAlertButton,样式:UIAlertActionStyle.Default,处理程序:nil))
// 添加视图 // 添加 self.view 子视图到 controller.view
// 显示警报 self.presentViewController(警报,动画:真,完成:无)
我猜你的 ViewControllerForLogin 已经出现了,或者它在故事板中没有 segue。