How to find out what UITextField caused a UIKeyboardWillShowNotification?

后端 未结 4 953
春和景丽
春和景丽 2021-02-19 04:42

I am trying to use a customized keyboard in my application, but I am hitting problems when trying to restrict it to one particular UITextField.

I based my code on this X

相关标签:
4条回答
  • 2021-02-19 05:16

    That project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too.

    You can set the keyboard type for a UITextField instance, e.g.:

    [myTextField setKeyboardType:UIKeyboardTypeDefault];
    

    or:

    myTextField.keyboardType = UIKeyboardTypeDefault;
    

    Search the Xcode help on UITextInputTraits Protocol for a list of UIKeyboardType constants.

    0 讨论(0)
  • Couldn't get any of the above to work. Had to do it manually:

    if ([companyName isFirstResponder]) {
        // ...............
    }
    else if ([notes isFirstResponder]) {
        //.............
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:29

    You can write a UIView category method to find the first responder.

    - (UIView *)firstResponder
    {
        if ([self isFirstResponder])
        {
            return self;
        }
    
        for (UIView *view in self.subviews)
        {
            UIView *firstResponder= [view firstResponder];
            if (firstResponder)
            {
                return firstResponder;
            }
        }
    
        return nil;
    }
    

    Then in your - (void)keyboardWillShow:(NSNotification *)notification method you can use it like this

      UITextField *textField = (UITextField *)[self firstResponder];
    
    0 讨论(0)
  • 2021-02-19 05:30

    I have achieved this before by comparing the object with the UI object that I am interested in. Like this:

    if(notification.object == exampleViewController.textField)
    {
      ...
    }
    
    0 讨论(0)
提交回复
热议问题