I am creating a crossword puzzle-type app, and am using the following code to make the grid:
- (void) viewDidLoad{
[...]
//we are inside 2 loops (puzzleRow and i), 15 rows (puzzleRow), 15 cols (i)...
UITextField *inputBox = [[UITextField alloc] initWithFrame:letterFrame];
key = (puzzleRow * 100) + i;
[inputBox setTag:key];
[....]
}
Later, I want to reference the UITextField and get their value, check it against the correct answer, etc.
- (IBAction)checkSolutionButton:(id)sender {
int i = 1;
for (id obj in self.Puzzle) {
int len = [obj length];
for(int j = 0; j < len; j++){
NSRange range = NSMakeRange(j, 1);
NSString *answer = [obj substringWithRange:range];
int key = (i * 100) + j;
UIView *userGuessSquare = [self.view viewWithTag:key];
UITextField *userGuessTextField = [userGuessSquare viewWithTag:key];
NSString *guess = userGuessTextField.text;
NSLog(@"guess: %@", guess);
}
}
}
self.Puzzle is an NSArray of strings (ANSWER1..ANSWER2,ANSWER3..ANSWER4) constituting a crossword puzzle’s rows, dots are black squares. The UIView are holders for the UITextField, each UIView and its UITextField has an identical key.
Here I am stuck. I get a warning Incompatible pointer types initializing UITextField with an expression type UIView I thought it was a subclass?
NSString *guess = userGuessTextField.text;
is a no go.
QUESTION:
How can I make a bunch of UITextField(s) and then access them and their values later?
(EDITED to include entire IBAction method).
Using the tags should be fine. To get the UITextField you should be doing this:
This will cast the UIView to the more specific UITextField, which is ok since you know you’ve used it. Make sure your tags start above 0 as well.