如何知道哪个UITextField响应?

时间:2013-12-02 06:59:42

标签: ios objective-c uitextfield uitextview

UIViewController我将自己添加为观察者:

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    ......
}

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];

    if (textField1.isFirstResponder) {
        NSLog(@"I am here1");
    }

    if (textField2.isFirstResponder) {
        NSLog(@"I am here2");
    }

     ........

}

我还在控制器中创建了五个UITextField。我的问题是如何知道调用哪个textField。我尝试使用该方法找出哪个“isFirstResponder”已更改,但它无法正常工作。

3 个答案:

答案 0 :(得分:4)

嗯...为什么不使用-textFieldDidBeginEditing:

UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%@",textField);
}

答案 1 :(得分:1)

使用标签属性!!当您创建Textfield时,设置他们的标签(某些特定于文本字段的数字),当您收到通知时,只需检查他们有哪个标签!

答案 2 :(得分:0)

尝试使用UIKeyboardDidShowNotification而不是UIKeyboardWillShowNotification。

相关问题