如何使用IBAction清除UITextField中的文本?

时间:2013-01-16 17:58:35

标签: ios uitextfield ibaction

我认为这很简单,但显然不是,至少不是我。我在UITextField文件中声明了.h并声明了其属性,以及IBAction来清除字段中的文本。

IBOutlet UITextField *textField1;
@property(nonatomic, retain) UITextField *textField1;
-(IBAction)clearText:(id)sender

然后在我的实现文件(.m)中,这就是我编写清除文本的方法。

-(IBAction)clearText:(id)sender {
   textField1.text = NULL;
}

但是当我按下模拟器中的按钮时没有任何反应。为什么呢?

2 个答案:

答案 0 :(得分:3)

你真的连接了插座吗?

另外,我建议使用nil@""代替NULL

答案 1 :(得分:0)

这必须有效:

textField1.text = @"";

进一步检查textFiel1的outled是否已连接(XCode中的黑色填充圆圈)
添加日志语句到

-(IBAction)clearText:(id)sender {
   NSLog(@"clearText");
   textField1.text = @"";
}

查看是否被调用。
如果未调用,请同时检查clearText:(id)sender的支出。