Bill Norman
Bill Norman

Reputation: 881

iOS keyboard and textview error - "use of undeclared variable"

I'm trying to follow the steps in Apple's "Text, Web and Editing Programming Guide for iOS" for Moving Content That Is Located Under the Keyboard (p. 36 of the PDF printout), and I keep getting an error in the code that I copied directly from the page (Listing 4-1) which includes this snippet under keyboardDidShow:():

    if (!CGRectContainsPoint(aRect, activeField.frame.origin)){
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
}

and then listing 4-2:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

(I do have Notifications in place and tested to be working.)

The problem is that Xcode flags the term activeField as an undeclared identifier. Obviously that means I need to declare that somewhere, but as much digging as I have done, I can't figure out where to declare it and how to declare it.

I have seen in other places where people have used this same code and didn't have a problem. Why me???

For you Xcode veterans, this is probably no big deal, so I am hoping you can educate me.

Thanks!

Upvotes: 1

Views: 878

Answers (1)

alberto acosta
alberto acosta

Reputation: 11

Just declare the variable in the main view controller:

@implementation MainViewController {
    UITextField *activeField;
}

Upvotes: 1

Related Questions