开发者

Passing text value from one view to another

开发者 https://www.devze.com 2023-01-19 18:30 出处:网络
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.

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];
开发者_StackOverflow社区    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 nameField is a subview. But clearly at the point that your code is trying to set its text property, the text field and its parent view haven't been loaded yet, so you're sending a -setText: message to nil.

Instead, add a property to your modal view controller of type NSString *, and set that in your buttonTapped implementation. Then, in the modal view controller's -viewWillAppear method, you can take the value from the property and put it into the text field.

0

精彩评论

暂无评论...
验证码 换一张
取 消