当用户在另一个UItxextField中切换时,textFieldShouldEndEditing不会触发

时间:2014-01-23 12:50:19

标签: ios uitextfield

我有textFieldShouldEndEditing的代码:

-(void)textFieldDidEndEditing:(UITextField *)textField
{
   NSDecimalNumber *testIfNumber = [[NSDecimalNumber alloc] initWithString:[textField text]];
   if (![testIfNumber isEqualToNumber:[NSDecimalNumber notANumber]])
   {
       NSLog(@"%@",[testIfNumber stringValue]);
       [[self labelTotal]setText:[self getTotalforRate:[expenseCategorySelected categoryRate] forAmmountField:textField]];

   }


}

现在我的问题是,在我的ViewController中,我有两个或更多的textField,当用户直接从一个到另一个textField时,不会触发此方法。

是否有方法或方法知道我何时从textField转到另一个? THX。

2 个答案:

答案 0 :(得分:3)

你错了...... textFieldShouldEndEditing不是为了这个目的。 只需使用textFieldDidEndEditing

答案 1 :(得分:0)

如果你没有得到这样的话


  #import "ViewController.h"

  @interface ViewController ()<UITextFieldDelegate>//heare set the delegate

  @end

  @implementation ViewController

  - (void)viewDidLoad
  {
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

     UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(30, 40, 200, 35)];
     UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(30, 80, 200, 35)];
     UITextField *textField3 = [[UITextField alloc]initWithFrame:CGRectMake(30, 125, 200, 35)];

     textField1.placeholder = @"I am 1st field";
     textField2.placeholder = @"I am 2nd field";
     textField3.placeholder = @"I am 3rd field";

     //set the delegate for all text fields
     textField1.delegate = self;
     textField2.delegate = self;
     textField3.delegate = self;

     textField1.tag = 100;
     textField2.tag = 200;
     textField3.tag = 300;

     [self.view addSubview:textField1];
     [self.view addSubview:textField2];
     [self.view addSubview:textField3];
  }

   //implement the delegate methods

   - (void)textFieldDidEndEditing:(UITextField *)textField
  {

     if(textField.tag == 100)
     {
       NSLog(@"i am end editing -> %@",textField.text);
     }
     else if (textField.tag == 200)
    {
       NSLog(@"i am end editing -> %@",textField.text);
    }
     else if (textField.tag == 300)
    {
        NSLog(@"i am end editing -> %@",textField.text);
    }   
  }


希望这有助于你.. :))