Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Can't pass string to another controller

I have an app that have table view controller and detail view controller. I want to pass value of one string to another string in detail controller. Im using storyboard, in fact, when i didn't use it it worked, but in another app.Im stacked here and can't understand what I'm doing wrong. There is my code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailViewController     *detailController = [[DetailViewController alloc]init];
    detailController.descriptionStringShort = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"description"];
    NSLog(@"%@", [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"description"]);

}

In console i can see value when i tap the row (in first-table controller), but when i push to another view i can only see nil in console:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myScrollView.contentSize = CGSizeMake(320, 960);
    self.myTextView.text = self.descriptionStringShort;
    [self.myScrollView addSubview:self.myTextView];
    NSLog(@"%@ descriptor", self.descriptionStringShort);

}

And there is nil in console. What am i doing wrong?

Any advice would be appreciated, thanks!

Upvotes: 0

Views: 83

Answers (3)

Jorn
Jorn

Reputation: 1044

You should not create a new instance of DetailedViewController when using segues.

Implement the method prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"YourSegueID"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"description"];
        DetailedViewController *DVC = segue.destinationViewController; //Might need a cast
        DVC.descriptionStringShort = string;
       }
}

Note: This code is not type checked, however, it should give you the idea of how to do this.

Upvotes: 2

Paul.s
Paul.s

Reputation: 38728

It looks like you are configuring the wrong object.

In your tableView:didSelectRowAtIndeXPath: you create a DetailViewController and configure it but this will not be the same instance of DetailViewController that you are segueing with. The instance you are configuring will actually be gone by the end of the method as ARC will see no other objects have a strong retain on it.

This is equivalent to ringing a bank and giving them all of your details to ask for a loan and then later ringing another bank and expecting them to give you a quote based on the details you gave to the first bank. It's always important to remember that your have to configure the specific object you want to use

You will need to configure the instance of DetailViewController you get during the segue

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

I suspect you might have two separate "DetailViewController" objects being created here.

One in your storyboard, and one that you're explicitly creating via the "alloc" and "init" in your code.

Also, view controllers are usually created using "initWithNibName:bundle" initializer.

Upvotes: 1

Related Questions