Swift 中无法从 UIView 显示 AlertController
Unable to present AlertController from a UIView in Swift
我试图在 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);
}
}
上面代码块中的注释行是原始代码块附带的内容,注释下面的行是我尝试替换的代码。谁能看出我做错了什么?
根视图控制器是否已经呈现了一个视图控制器?如果是这样,您可能需要使用:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController?.presentViewController(alert, animated: true, completion: nil)
话虽如此,使用委托模式并让视图告知管理它的任何视图控制器以呈现警报视图控制器可能更容易(并且更多 consistent/appropriate)。
在Swift5
let window = UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first
window?.rootViewController?.presentedViewController?.present(alert, animated: true, completion: nil)
对于 swift 5 :
UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.present(alertName, animated: true, completion: nil)
我试图在 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);
}
}
上面代码块中的注释行是原始代码块附带的内容,注释下面的行是我尝试替换的代码。谁能看出我做错了什么?
根视图控制器是否已经呈现了一个视图控制器?如果是这样,您可能需要使用:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController?.presentViewController(alert, animated: true, completion: nil)
话虽如此,使用委托模式并让视图告知管理它的任何视图控制器以呈现警报视图控制器可能更容易(并且更多 consistent/appropriate)。
在Swift5
let window = UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first
window?.rootViewController?.presentedViewController?.present(alert, animated: true, completion: nil)
对于 swift 5 :
UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.present(alertName, animated: true, completion: nil)