输入UITextField的值作为击键? [英] Getting the Value of a UITextField as keystrokes are entered?

查看:87
本文介绍了输入UITextField的值作为击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

IBOutlet UITextField* nameTextField;
IBOutlet UILabel* greetingLabel;

我想让 greetingLabel

我需要的基本上是iPhone等效的Cocoa委托方法 controlTextDidChange 。

What I need basically is the iPhone equivalent of the Cocoa delegate method controlTextDidChange.

每当发生键击时,会调用 textField:shouldChangeCharactersInRange:委托方法:

The textField:shouldChangeCharactersInRange: delegate method is called each time a keystroke occurs:

- (BOOL)              textField:(UITextField *)textField
  shouldChangeCharactersInRange:(NSRange)range     
              replacementString:(NSString *)string

string参数返回被按下的字符。然而,实际的 textField 的值( nameTextField.text )仍然为空。

The string argument returns the character that is pressed. The actual textField's value (nameTextField.text) remains blank however.

我在这里缺少什么? (我想要 nameTextField 以反映用户到目前为止输入的确切字符串)。

What am I missing here? (I'd like nameTextField to reflect the exact string that the user has entered so far).

推荐答案

事实证明,最简单的方法是使用Interface Builder:

It turns out, the easiest way to do this is using Interface Builder:


  • 添加IBAction

  • 在界面生成器中的UITextField上点击(或右键点击)

  • 连接编辑

工作原理就像一个魅力:)(我不敢相信我花了很多天的时间,现在认识到解决方案比我想象的简单得多:P)

Works like a charm :) (I can't believe I spent numerous days on this, and to realize now that the solution was much simpler than I'd thought :P)

这篇关于输入UITextField的值作为击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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