gpt4 book ai didi

ios - 从 UIAlertView 文本字段获取文本的问题

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:20 25 4
gpt4 key购买 nike

在我的应用程序中,我想要一个带有文本字段的警报。单击“完成”后,我想将文本字段输入保存在一个字符串中。单击“取消”后,我只想关闭警报。我已经像这样创建了我的警报:

    var alert = UIAlertView()
alert.title = "Enter Input"
alert.addButtonWithTitle("Done")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("Cancel")
alert.show()

let textField = alert.textFieldAtIndex(0)
textField!.placeholder = "Enter an Item"
println(textField!.text)

警报看起来像这样:

My alert

我想知道如何从文本字段中获取文本,以及如何为“完成”按钮和“取消”按钮创建事件。

最佳答案

您可以使用 UIAlertController 而不是 UIAlertView。

我已经使用 UIAlertController 实现并测试了您真正想要的东西。请尝试以下代码

    var tField: UITextField!

func configurationTextField(textField: UITextField!)
{
print("generating the TextField")
textField.placeholder = "Enter an item"
tField = textField
}

func handleCancel(alertView: UIAlertAction!)
{
print("Cancelled !!")
}

var alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: .Alert)

alert.addTextFieldWithConfigurationHandler(configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler:{ (UIAlertAction) in
print("Done !!")

print("Item : \(self.tField.text)")
}))
self.presentViewController(alert, animated: true, completion: {
print("completion block")
})

关于ios - 从 UIAlertView 文本字段获取文本的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com