views:

1456

answers:

4

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?

A: 

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.

Alex Reynolds
I specifically set the other UITextField to use different keyboard types. Unfortunately the 'custom keyboard' code modifies any keyboard which appears, and I can't figure out how to distinguish which UITextField caused the UIKeyboardWillShowNotification notification to occur. I've updated the post to clarify that a bit more.
Dan J
+2  A: 

There is a new version of this project on my new site.

Here is the direct link. It uses multiple Text Fields. http://brygruver.squarespace.com/customnumberpad/

Bryan S. Gruver
Using the UITextField .editing property to find out which field is currently being edited and hiding the custom button works great. Nice solution! I had tried adding a textFieldDidBeginEditing delegate to remember which field was being edited, but it was always getting called after the UIKeyboardWillShowNotification.
Dan J
Looks like a good solution. I had to distinguish between a UITextField and a UITextView, and the latter doesn't have an "editing" property. Instead, I'm using "[textView isFirstResponder]" to check if the text view was the view that triggered the keyboard. Works great.
Mirko Froehlich
Link changed..http://brygruver.squarespace.com/blog/2009/10/1/creating-a-custom-number-pad.html
Sijo
A: 

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) { ... }

John S. Eddie
A: 

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

if ([companyName isFirstResponder]) {
    // ...............
}
else if ([notes isFirstResponder]) {
    //.............
    }
}
Ajay Gautam