I was wondering if you could help me.
I want to create a situation where if a user enters a specific text in a textfield – they are taken to a new view.
I am struggling to get this to work – I use the following code to instigate this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@"\n"]) {
NSString *lowercaseactualanswer = [footballersurname lowercaseString];
NSString *lowercaseuseranswer = [textField.text lowercaseString];
if ([lowercaseuseranswer isEqualToString:lowercaseactualanswer])
{
CorrectScreen *screen = [[CorrectScreen alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
else {
self.guess.text = [[NSString alloc]initWithFormat: @"%@ is incorrect", textField.text];
textField.text = @"";
}
}
return YES;
}
and I have used the same method to navigate to different screens elsewhere in my code with success (using the same method as from the three lines from CorrectScreen *screen) The error message I receive is:
-[GameScreen textFieldDoneEditing:]: unrecognized selector sent to instance 0x6c43e20
and when adding an exception breakpoint – the green arrow stops at the line starting [self presentModal….] with the thread highlighting the following:
-[GameScreen textField:shouldChangeCharactersInRange:replacementString:]
I don’t really know where the problem lies. I was under the impression that an unrecognised selector problem usually relates to not linking up objects correctly but I am certain I have that done here.
Does anyone have any ideas? I am a bit stuck at the moment and I’m sure someone out there can help!
Thanks very much!
Andy
I suspect that what happens is the following:
you are in a text field;
when the field content changes, you present a modal view;
presenting a modal view will try to end the edit session by sending
textFieldDoneEditingto yourGameScreenobject;textFieldDoneEditingis not defined forGameScreen, so your program crashes.The fix is defining that method for your class.
Since
textFieldDoneEditingis not part of the iOS SDK (as far as I can see), I assume that you defined it in Interface Builder to be an action that is fired by your textfield “Editing Did End” event. You could also appropriately modify this binding so fix the problem.