0

I'm trying to connect the text fields in a sign up form so that, upon done being pressed for each, the next field in the form automatically becomes active. To achieve this, I've extended the UITextField class, as follows, in the view controller for the sign up view.

@interface UITextField (Extended)
    @property (nonatomic, weak) IBOutlet UITextField* nextField;
@end

I've then set the nextField outlets via interface builder for each field in the form, and have the following code implemented in the view controller, which is also acting as the delegate for the text fields in the form.

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    UITextField* field = textField.nextField;
    if (field)
        [field becomeFirstResponder];
    return NO;
}

However when building and running the app, it immediately crashes giving the following error :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason:'[<UITextField 0x78729580> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key nextField.'

A bit lost as to how to address this, any help would be appreciated! Thanks in advance.

Alex P
  • 530
  • 1
  • 6
  • 13

2 Answers2

1

Just remove all the outlets and recreate them again. You just missed or duplicated one of them, since you have several textfields it is highly possible something has gone awry. And make sure that each nextField really points to next field :)

UPDATE: It's late night so I missed one big problem. You can't add new properties via category to an existing class like that. The reason is category is not capable of creating a backing variable behind the scenes. (There is a way with associated objects..but it's best not to delve into that black magic.)

Instead of UItextField+extended category, subclass the UITextfield class.

@interface CustomTextField : UITextField

@property (nonatomic, strong) IBOutlet UITextField *nextField;

@end

Then in interface builder, you will have to set that CustomTextfield class in the class inspector for each text field.

Earl Grey
  • 7,426
  • 6
  • 39
  • 59
  • I've tried removing and reconnecting the outlets multiple times, and the issue persists. Do all outlets have to be connected to something? Will an unconnected outlet cause a problem? – Alex P Feb 20 '15 at 23:06
  • Check the Update: in this answer. – Earl Grey Feb 21 '15 at 01:04
  • This did it! Allowed them to be set via IB, worked like a charm. Thanks! :) – Alex P Feb 21 '15 at 20:42
0

The problem is that while you can declare a property in a category, the compiler will not define the property getter and setter for you. You have to define the getter and setter yourself.

You can use an “associated object” to store the property value. See this answer for example code. Since you don't want your nextField property to retain the next field, use OBJC_ASSOCIATION_ASSIGN instead of OBJC_ASSOCIATION_RETAIN_NONATOMIC when setting the associated object.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848