2 exit door eXit_door 于 2013.05.29 16:13 提问

自动调用textDidChange方法

在我的iPhone应用中有一个textfield,我希望在文本被改动的时候下面的方法能自动调用:

-(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];

3个回答

KindAzrael
KindAzrael   2013.05.31 00:38

你可以使用UITextField 的delegate中的方法
textField:shouldChangeCharactersInRange:replacementString:

zhanglei5415
zhanglei5415   Rxr 2013.05.29 16:20

改成

[[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(textFieldTextDidChangeOneCI:)
                                              name:UITextFieldTextDidChangeNotification
                                            object:YOUR_TEXT_FIELD];
abcdeFGh_xyz
abcdeFGh_xyz   2013.05.29 16:45

用我方法:

 - (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);

 }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!