[英]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.