我有几个 UITextField
和一个 UIButton
。如果我点击 UIButton
,我想在事件的 UITextField
中放置一些静态文本。
所以像这样:
@IBAction func buttonEen(sender: UIButton) {
'active textField'.text = "static text"
}
但是我如何确定哪个 UITextField
是事件的 UITextField
以及如何到达它?
最佳答案
要在最后一个事件的 UITextField 中写入文本,您必须让您的 UIViewController
成为 UITextField
的委托(delegate)
例如:
class ViewController: UIViewController, UITextFieldDelegate
声明一个activeField
变量
例如:
var activeField: UITextField?
然后实现textFieldDidBeginEditing
例如:
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
所以你的按钮功能会是这样的
@IBAction func buttonEen(sender: UIButton) {
if activeField
{
activeField.text = "static text"
}
}
关于swift - 如何将文本添加到事件的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400367/