简体   繁体   English

如何从视图中呈现UIAlertController?

[英]How to present a UIAlertController from a view?

Each cell in my collection view has a button and a textfield. 我的集合视图中的每个单元格都有一个按钮和一个文本字段。

When the button is pressed I want to display an alert if the text field is empty. 按下按钮时,如果文本字段为空,我想显示一个警报。

In my custom collection view cell I have: 在我的自定义集合视图单元格中,我有:

func displayAlert(title: String, error: String) {
 var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
 alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
 alert.dismissViewControllerAnimated(true, completion: nil)
 }))
 self.presentViewController(alert, animated: true, completion: nil)
}

func redeemButtonPressed(sender: UIButton!) {
 if enterCodeTextField.text == "" {
  displayAlert("Something went wrong", error: "Please enter a valid code")
}

尝试使用alert.show()作为显示警报的方式。

Try this for your function: 尝试以下功能:

func redeemButtonPressed(sender: UIButton!) {
if enterCodeTextField.text == " "{

        let alert = UIAlertView()
        alert.title = "Alert Title here"
        alert.message = "Message Here"
        alert.addButtonWithTitle("Understood")
        alert.show()

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 UIAlertController 显示警报 - How to present an alert with UIAlertController 如何完全从视图中解脱uialertcontroller? - How to dismiss uialertcontroller from view completely? 尝试在视图不在窗口中的UIAlertController上显示OTPViewController - Attempt to present OTPViewController on UIAlertController whose view is not in window 尽管UIAlertController在视图层次结构中,但仍显示ViewController - Present a ViewController although UIAlertController is in the view Hierarchy 无论视图层次如何,都将 UIAlertController 呈现在所有内容之上 - Present UIAlertController on top of everything regardless of the view hierarchy 尝试呈现<uialertcontroller>其视图不在 window 层次结构中</uialertcontroller> - Attempt to present <UIAlertController> on whose view is not in the window hierarchy 尝试显示其视图不在窗口层次结构中的UIAlertController - Attempt to present UIAlertController whose view is not in the window hierarchy 如何修复在视图不在 window 层次结构中的 ViewController 上呈现 UIAlertController 的尝试 - How can i fix attempt to present UIAlertController on ViewController whose view is not in the window hierarchy 如何呈现位于另一个UIViewController中的UIAlertController - how to present UIAlertController located in another UIViewController 如何在 SwiftUI 中显示 UIAlertController? - How do I present a UIAlertController in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM