I have followed the first steps tutorial from apple, and I have a working app that can read text and display it in a label on button click. When I hit enter the textfield is resigned and the keyboard disappears, and when I hit the button the changeGreeting method is invoked. I want to call the changeGreeting function in the function thats used when I hit enter which is textFieldShouldReturn.
I tried everything I could think of, and read a lot online but Im not sure how to deal with (id)sender as a param for example. How should I edit my code to call changeGreeting on textfield enter?
code below:
- (IBAction)changeGreeting:(id)sender {
self.userName = self.textField.text;
NSString *nameString = self.userName;
NSString *endString = @"burp";
int r = arc4random() % 74;
if ([nameString length] == 0) {
nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@, %@! Random String of numbers: %d", nameString, endString, r];
self.label.text = greeting;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
// on enter the keyboard is removed, but I want the
// changeGreeting method involed too, something like
// [self changeGreeting]
}
return YES;
}
thanks in adv
You already have it. Just ignore the
senderparameter: