Question

I'm running into a little issue with my textFieldDidBeginEditing method..

I'm trying to figure out which textfield is being called on to edit so I can decide if I want the view to move up or not to make the field visible.

Here is my method, I have commented some things out to try to find out where the error is:

- (void)textFieldDidBeginEditing:(UITextField *)sender
{
NSLog(@"This method is called");
//[self.view setFrame:CGRectMake(0,-120,320,568)];
if(sender.tag == _nameF.tag)
{
    NSLog(@"This if is called");
    //[self.view setFrame:CGRectMake(0,-120,320,568)];
}

else
{
    NSLog(@"Else called instead");
}

}

I see "This method is called" in the log, so I know the method is being called in the first place, but after that, I see this:

2013-07-23 12:27:18.654 SidebarDemo[2110:60b] -[NSConcreteNotification tag]: unrecognized selector sent to instance 0x15d7b8c0 2013-07-23 12:27:18.655 SidebarDemo[2110:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteNotification tag]: unrecognized selector sent to instance 0x15d7b8c0'

This leads me to believe that it is something with sender.tag, but I don't see anything wrong with my code, to my knowledge.

What could the issue be here? Is there another method I can use to find out what textfield is being edited?

Thanks.

Was it helpful?

Solution

Since you are setting up the UITextFieldTextDidBeginEditingNotification notification to call your textFieldDidBeginEditing: method, you need to change the method parameter. And to avoid confusion with the corresponding UITextFieldDelegate method, you should rename this method as well (which means you need to update the line of code that register the notification handler).

- (void)textFieldDidBeginEditingHandler:(NSNotification *)notification {
    UITextField *textField = (UITextField *)notification.object;

    // It's OK to use == here since we really do want to compare pointer values
    if(textField == _nameF) {
        NSLog(@"This if is called");
        //[self.view setFrame:CGRectMake(0,-120,320,568)];
    } else {
        NSLog(@"Else called instead");
    }
}

There is no need for tags since you have ivars for each text field.

BTW - why are you using notifications for this? Why not use the UITextFieldDelegate methods?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top