简体   繁体   English

如何知道哪个UITextField在响应?

[英]How to know which UITextField is responding?

In the UIViewController I add itself as observer: 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");
    }

     ........

}

I also create five UITextField in the controller. 我还在控制器中创建了五个UITextField My problem is how to know which textField is called. 我的问题是如何知道哪个textField被调用。 I try the method to find out whose "isFirstResponder" have change , but it is not working. 我尝试使用该方法来找出谁的“ isFirstResponder”已更改,但无法正常工作。

erm... why don't you use -textFieldDidBeginEditing: of UITextFieldDelegate erm ...为什么不使用UITextFieldDelegate -textFieldDidBeginEditing:

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

Use the tag property!! 使用标签属性! When you create your Textfield set their tags (to some textfield-specific number), and when you get the notification, simply check which tag they have! 当您创建文本字段时,请设置其标签(为某个特定于文本字段的编号),并在收到通知时,只需检查它们具有的标签即可!

尝试使用UIKeyboardDidShowNotification而不是UIKeyboardWillShowNotification。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM