我们如何在没有用户输入的情况下创建和关闭 UIAlertController? (迅速)
Posted
技术标签:
【中文标题】我们如何在没有用户输入的情况下创建和关闭 UIAlertController? (迅速)【英文标题】:How do we create and dismiss an UIAlertController without user input? (Swift) 【发布时间】:2015-02-04 03:52:30 【问题描述】:我一直在查找很多关于 UIAlertController 的教程。到目前为止,我发现的方法是通过将 UIAlertController 链接到按钮或标签来激活它,然后调用 IBAction。
我尝试复制代码以在用户进入应用程序时自动弹出警报(我想问用户是否想通过教程)。但是,我不断收到错误消息:
警告:尝试在其视图不在窗口层次结构中的 MainViewController 上呈现 UIAlertController!
然后我尝试通过 addChildViewController 和 addSubview 将 UIAlertController 添加到 MainViewController。但是,我得到了错误:
应用程序试图以模态方式呈现一个活动控制器
我发现我不能使用 presentViewController 函数并将其注释掉。
UIAlertController 显示,但是当我尝试单击取消或从不按钮时,会发生此错误。
试图用未知的演示者关闭 UIAlertController。
我真的很难过。有人可以分享我做错了什么吗?非常感谢。这是代码。
func displayTutorial()
alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
self.addChildViewController(alertController)
self.view.addSubview(alertController.view)
alertController.didMoveToParentViewController(self)
alertController.view.frame.origin.x = self.view.frame.midX
alertController.view.frame.origin.y = self.view.frame.midY
//alertController.popoverPresentationController?.sourceView = self.view*/
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) (action) in
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Destructive) (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) (action) in
self.tutorial = 1
alertController.addAction(neverAction)
//self.presentViewController(alertController, animated: false)
【问题讨论】:
【参考方案1】:我找到了解决方案。显然,我不能从 func viewDidLoad 调用 UIAlertController。我必须从 viewDidAppear 调用该函数。所以我现在的代码是
override func viewDidAppear(animated: Bool)
if tutorial == 0
displayTutorial(self.view)
func displayTutorial(sender:AnyObject)
let alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) (action) in
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Default) (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) (action) in
self.tutorial = 1
alertController.addAction(neverAction)
self.presentViewController(alertController, animated: true, completion: nil)
if let pop = alertController.popoverPresentationController
let v = sender as UIView
pop.sourceView = view
pop.sourceRect = v.bounds
感谢这个帖子:Warning: Attempt to present * on * whose view is not in the window hierarchy - swift
【讨论】:
【参考方案2】:带有扩展名的 UIAlertController 下方将帮助您显示具有动态按钮数量的警报,并带有所选索引的完成处理程序
extension UIViewController
func displayAlertWith(message:String)
displayAlertWith(message: message, buttons: ["Dismiss"]) (index) in
func displayAlertWith(message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void
displayAlertWithTitleFromVC(vc: self, title: Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String, andMessage: message, buttons: buttons, completion: completion)
func displayAlertWithTitleFromVC(vc:UIViewController, title:String, andMessage message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
for index in 0..<buttons.count
let action = UIAlertAction(title: buttons[index], style: .default, handler:
(alert: UIAlertAction!) in
if(completion != nil)
completion(index)
)
alertController.addAction(action)
DispatchQueue.main.async
vc.present(alertController, animated: true, completion: nil)
如果您需要自动关闭警报,您可以在延迟一段时间后在呈现的视图控制器上调用 dismiss。
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1)
vc.dismiss(animated: true, completion: nil)
希望这对你有帮助。
【讨论】:
以上是关于我们如何在没有用户输入的情况下创建和关闭 UIAlertController? (迅速)的主要内容,如果未能解决你的问题,请参考以下文章
如何让用户在不使用输入字段的情况下将文本输入 <li>? [关闭]