I have a UIView subclass that I assign to a text field as follows:
self.textField.inputView = [[HexKeyboard alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
and this works (i.e., the keyboard comes up). However, how should the HexKeyboard instance know about the textField?
[Of course I can add a property to the HexKeyboard to achieve this (and call it delegate), but I figure there’s a built-in mechanism for this…]
There seems to be no built-in mechanism for this, as the other answerers have pointed out. As Nick says, you don’t need a complex delegate pattern for this. Or rather, you use the delegate pattern, but you get the delegate class for free. In this case it’s the
UITextInputprotocol.So your keyboard probably looks like this (and has a NIB)
When you create the keyboard controller, you assign the
UITextInputconformer to it, so something like this:However, I thought, there MUST be a way to define this keyboard just once and get the keyboard to “automatically know” who the
UITextInputobject that summoned it is. But I’ve looked around to no avail… you cannot figure out who thefirstResponderis unless you troll the view hierarchy yourself or retain your delegates in a list (which would cause a retain loop). Plus, this isn’t so bad because the HexKeyboardController will unload, too, when thetextFieldis dealloced.