如何将文本添加到活动的 UITextField [英] How to add text to an active UITextField

查看:38
本文介绍了如何将文本添加到活动的 UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 UITextField 和一个 UIButton.如果我点击 UIButton,我想在活动的 UITextField 中放置一些静态文本.

比如:

@IBAction func buttonEen(sender: UIButton) {'active textField'.text = "静态文本"}

但是我如何确定哪个 UITextField 是活动的 UITextField 以及如何到达它?

解决方案

要在最后一个活动的 UITextField 中写入文本,您必须使您的 UIViewController 成为 UITextField 的委托>

例如:

class ViewController: UIViewController, UITextFieldDelegate

声明一个 activeField 变量

例如:

var activeField: UITextField?

然后实现textFieldDidBeginEditing

例如:

func textFieldDidBeginEditing(textField: UITextField){活动字段 = 文本字段}

所以你的按钮功能将类似于

@IBAction func buttonEen(sender: UIButton) {如果 activeField{activeField.text = "静态文本"}}

I have a couple of UITextField's and one UIButton. If I tap on the UIButton, I want to place some static text in the active UITextField.

So something like:

@IBAction func buttonEen(sender: UIButton) {
    'active textField'.text = "static text"
}

But how do I determine which UITextField is the active UITextField and how do I reach it?

解决方案

To write text in last active UITextField you have to make your UIViewController a delegate of UITextField

ex:

class ViewController: UIViewController, UITextFieldDelegate

declare a activeField variable

ex:

var activeField: UITextField?

then implements textFieldDidBeginEditing

ex:

func textFieldDidBeginEditing(textField: UITextField)
{
    activeField = textField
}

So your button function will be something like

@IBAction func buttonEen(sender: UIButton) {
if activeField
    {
        activeField.text = "static text"
    }

}

这篇关于如何将文本添加到活动的 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆