首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法UItextfield -在我输入UITextField之后

方法UItextfield -在我输入UITextField之后
EN

Stack Overflow用户
提问于 2013-11-04 02:32:11
回答 4查看 1.5K关注 0票数 0

我正在寻找一种UITextField方法,它在输入UITextField之后生成一个东西。

我有一个方法来进行计算,我使用一个按钮,但是我不想使用,我希望您在UITextField中输入数字之后运行这个方法。

对哪种方法有什么建议吗?

谢谢你的帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-11-04 02:50:31

  1. 实现UITextFieldFieldDelegate协议
  2. 将文本字段的委托设置为视图控制器

self.textField.delegate = self;

然后实现shouldChangeCharactersInRange方法

代码语言:javascript
复制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        [self doCalculations];
        return NO;
    }
票数 3
EN

Stack Overflow用户

发布于 2013-11-04 02:47:44

您可以使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string立即获得输入的文本。或者,您可以使用- (BOOL)textFieldShouldReturn:(UITextField *)textField来捕捉用户点击返回并在那个时候从文本字段中读取文本。这两种方法都在UITextFieldDelegate协议中,其中的文档可以找到这里

编辑:

或者,您可以使用[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];并实现textChanged方法来捕获EditingChanged事件。

票数 1
EN

Stack Overflow用户

发布于 2013-11-04 05:22:06

代码语言:javascript
复制
//in your ViewContoller.h
//Implement UITextFieldFieldDelegate protocol


//In your ViewContoller.m
//where you are creating your textfield.
textField.delegate = self;

// There are 2 delegate method that you can implement
// this method will be called whwn ever you enter a a letter.
// say you enter 'abcd' then this method will be called 4 times.  
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//do your calculation if required
    return YES;
}

//this method will be called when the textField will end editing, i.e when the keyboard resigns of the control goes to some other textField/textView
- (void)textFieldDidEndEditing:(UITextField *)textField {

}

根据您的要求,您可以在第一或第二种方法中进行计算。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19760793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档