I have this in my table view controller, and i want to pass the cell’s text value to my UITextField on the modal view.
- (void)buttonTapped:(id)sender {
AddName *addName = [[AddName alloc] init];
UITableViewCell *cell = (UITableViewCell*)[sender superview];
NSLog(@"text in buttontapped: %@", cell.textLabel.text);
addName.nameField.text = cell.textLabel.text;
addName.delegate = self;
[self presentModalViewController:addName animated:YES];
}
The NSLog shows the correct cell text value.
On my modal view’s viewDidLoad method i have this… and the text value never gets set…
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"text: %@",nameField.text);
nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameField.autocorrectionType = UITextAutocorrectionTypeDefault;
[nameField becomeFirstResponder];
}
What’s going on?
Thanks.
It looks as though you’re assuming that the modal view controller has already loaded its view, since I’m assuming
nameFieldis a subview. But clearly at the point that your code is trying to set itstextproperty, the text field and its parent view haven’t been loaded yet, so you’re sending a-setText:message tonil.Instead, add a property to your modal view controller of type
NSString *, and set that in yourbuttonTappedimplementation. Then, in the modal view controller’s-viewWillAppearmethod, you can take the value from the property and put it into the text field.