0

Possible Duplicate:
viewDidLoad being called before didSelectRowAtIndexPath

I am trying to pass variables over to the new view. I have the following code:

appDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];

dataCenter.myVariable = [array objectAtIndex:indexPath.row];

in the

didSelectRowAtIndexPath

of the calling view. However, the issue that I have is that this variable is empty in the vewDidLoad function of the next view, simply because it fired off BEFORE the didSelectRowAtIndexPath of the calling view. I am using storyboard to link the views together. Both are UITableView.

If I hit back and then reselect the table element it is then set, granted that by the time I hit back and then selected again, the variable got set. Is there any way to for the order of execution? I really don't want to do UI view switching on the back end.

Any help is greatly appreciated!

Community
  • 1
  • 1
Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • This question has already been answered. Please see the possible duplicate section. I will leave this up here in case it helps someone find the right answer (if your search leads you here). Please vote to close this question. – Serguei Fedorov Nov 11 '12 at 23:06

1 Answers1

1

How about saving the data to an NSUserDefaults at didSelectRowAtIndexPath And then at viewDidLoad read that saved data from NSUserDefaults.

-(void) didSelectRowAtIndexPath {
    NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
    [infoSaved setObject:myVariable forKey:@"myvariable"];
    [infoSaved:synchronize];

    // then call the new view to load
}

And in the new view that's loaded:

-(void) viewDidLoad {
    NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
    myVariable = [infoSaved objectForKey:@"myvariable"];

    // and use your variable
}

Hope that helps

Shachar
  • 1,110
  • 9
  • 22