Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out what UITextField caused a UIKeyboardWillShowNotification?

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 Xcode project (originally found on this blog). That code adds a custom UIButton (representing a 'decimal point') into the UIKeyboardTypeNumberPad keyboard view. It does it by subscribing to UIKeyboardWillShowNotification and modifying the keyboard when it appears.

That Xcode project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too, even though I have selected a completely different keyboard type for that text field.

I attempted to register to only see UIKeyboardWillShowNotification notifications from the one particular UITextField, but that doesn't seem to work:

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

I also tried to inspect the object inside the NSNotification passed to keyboardWillShow, but unfortunately it refers to the keyboard, not the UI control that caused the keyboard to appear.

2009-10-21 19:50:22.205 Example[6302:207] NSConcreteNotification 0x321ebd0 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0.300000011920929;
UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};

}}

Am I misunderstanding the addObserver interface? Surely there must be a way to subscribe to notifications from a particular UI control?

Has anybody got any other suggestions on how to achieve this?

like image 966
Dan J Avatar asked Oct 22 '09 00:10

Dan J


2 Answers

Couldn't get any of the above to work. Had to do it manually:

if ([companyName isFirstResponder]) {
    // ...............
}
else if ([notes isFirstResponder]) {
    //.............
    }
}
like image 196
Ajay Gautam Avatar answered Oct 15 '22 20:10

Ajay Gautam


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];
like image 3
berilcetin Avatar answered Oct 15 '22 22:10

berilcetin