我正在寻找一种UITextField方法,它在输入UITextField之后生成一个东西。
我有一个方法来进行计算,我使用一个按钮,但是我不想使用,我希望您在UITextField中输入数字之后运行这个方法。
对哪种方法有什么建议吗?
谢谢你的帮助。
发布于 2013-11-04 02:50:31
UITextFieldFieldDelegate协议self.textField.delegate = self;
然后实现shouldChangeCharactersInRange方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self doCalculations];
return NO;
}发布于 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事件。
发布于 2013-11-04 05:22:06
//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 {
}根据您的要求,您可以在第一或第二种方法中进行计算。
https://stackoverflow.com/questions/19760793
复制相似问题