【发布时间】:2016-06-09 16:19:28
【问题描述】:
我试图在 Swift 中显示一个 AlertController,但不是来自 UIViewController,而是来自从其父 UIViewController 调用的 UIView。当我尝试调用控制器时,出现以下错误:
Warning: Attempt to present <UIAlertController: 0x7fa5544a3140> on
<UINavigationController: 0x7fa553830400> whose view is not in the window
hierarchy!
我尝试调用 alertController 的代码是这样的:
let logInButton = TWTRLogInButton { (session, error) in
if let unwrappedSession = session {
let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
//self.presentViewController(alert, animated: true, completion: nil)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
} else {
NSLog("Login error: %@", error!.localizedDescription);
}
}
上面代码块中的注释行是原始代码块附带的,注释下面的行是我试图替换它的代码。谁能看到我做错了什么?
【问题讨论】:
标签: ios swift uiview uialertcontroller