I have been searching and trying to work this out for a few hours. I know that I’m making a very simple mistake, I just don’t know what it is!
I am trying to get the text that’s entered into two different UITextFields and put them into a table of another TableViewController.
My NSLog is returning that my addGuestViewController is receiving the text input, but not transferring it into my guestlistViewController.
guestlistViewController.h
@interface guestlistViewController : UITableViewController {
NSString *firstnameInput;
NSString *lastnameInput;
}
@property (nonatomic, retain) NSString *firstnameInput;
@property (nonatomic, retain) NSString *lastnameInput;
@end
guestlistViewController.m
@synthesize firstnameInput;
@synthesize lastnameInput;
NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstnameInput, lastnameInput];
NSArray *array = [[NSArray alloc] initWithObjects: fullname, nil];
NSLog(@"Their name is: "%@", fullname);
addGuestListViewController.h
@interface addGuestViewController : UITableViewController
@property NSString *firstnameInput;
@property NSString *lastnameInput;
@end
addGuestListViewController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
firstnameInput = firstname.text;
lastnameInput = lastname.text;
if (textField == firstname) {
[textField resignFirstResponder];
[lastname becomeFirstResponder];
} else if (textField == lastname) {
[textField resignFirstResponder];
[self performSegueWithIdentifier:@"done" sender:self];
NSLog(@"Their name is: %@, %@", firstnameInput, lastnameInput);
}
return YES;
}
If someone could inform me on what I’m doing wrong, it’d be a huge help as I’m fairly new to programming in Objective-C.
EDIT:
I was thinking that because I have my segue pointing to a navigation controller, it could be causing a problem, but not sure.
You’re not setting the properties, firstnameInput and lastnameInput of your guestlistViewController — In the textFieldShouldReturn: method, you’re assigning values from the text fields to the addGuestListViewController’s properties, not to the ones in guestlistViewController. You need to implement the method prepareForSegue:sender:, and in that method you get a reference to guestlistViewController with segue.destinationViewController. With that reference, you can set the values of the properties in that view controller. Something like this: