iphone - 如何在iphone应用程序中自动调用UITextField的textDidChange方法

标签 iphone ios uitextfield

我正在 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/

相关文章:

objective-c - 一些初学 Objective-C/iPhone 的问题

ios - 父 View 中容器 View 的自动布局未与布局指南对齐

ios - 在 UIView 级别获取 UITextField Y 坐标?

iphone - 自定义 UITextField

iphone - @selector 中的参数

iphone - 使用 AVAssetReader 绘制波形

ios - react 性 cocoa : How to prevent RACSignal from being executed twice in parallel?

ios - 在for循环中将NSDictionary转换为NSMutableDictionary

ios - 滚动 UIScrollView 以适应 iOS 中的键盘

iOS - 从 tableViewController 到 ViewController