我正在 iPhone 应用程序中工作,其中有一个文本字段,我希望当任何文本更改时,以下方法应该自动调用。
-(void)textFieldTextDidChangeOneCI:(UITextField*)tf{
NSLog(@"Testing Successful Value %.2f",appDelegate.p_ClinInf_Yes_V_InModel);
appDelegate.p_ClinInf_Yes_NV_InModel = [[textFieldOne text] floatValue];
appDelegate.p_ClinInf_No_NV_InModel=100-appDelegate.p_ClinInf_Yes_NV_InModel;
textFieldTwo.text=[NSString stringWithFormat:@"%.2f",appDelegate.p_ClinInf_No_NV_InModel];
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
NSString*string=textFieldOne.text;
if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Only a number can be entered into this input field " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
textFieldOne.text=@"";
}
NSLog(@"Testing Successful CI Data %.2f",appDelegate.p_ClinInf_Yes_NV_InModel);
}
目前我正在调用此
使用
[textFieldOne addTarget:self action:@selector(textFieldTextDidChangeOneCI:) forControlEvents:UIControlEventEditingChanged];
最佳答案
试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(textFieldTextDidChangeOneCI:)
name:UITextFieldTextDidChangeNotification
object:textfield];
}
同时修改你的方法
-(void)textFieldTextDidChangeOneCI:(NSNotification *)notification
{
UITextField *textfield=[notification object];
NSLog(@"%@",textfield.text);
}
关于iphone - 如何在iphone应用程序中自动调用UITextField的textDidChange方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684916/