Author Topic: Passing information via segues  (Read 5562 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Passing information via segues
« on: June 16, 2014, 06:49:16 PM »
I've been experimenting with creating a side panel menu using a library called SWRevealViewController from GitHub, and I'm trying to update a label in the main front view controller based on the name selected from the side panel tableview controller using a segue between the two to see if I could pass information between them. This is my prepareForSegue method in the TableViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
   

    if ([[segue identifier] isEqualToString:@"segueFront"]) {
       
        ViewController *viewController = (ViewController *)segue.destinationViewController;
        viewController.label.text = name;
       
        NSLog(@"name is %@", name);
        NSLog(@"label text is %@", viewController.label.text);
       
    }

The console shows that the name variable is filled appropriately, but the viewController.label.text remains null, and the label on the ViewController storyboard doesn't get updated with the name selected from the tableview. Can you please tell me what I am doing wrong here? I have attached a copy of the project to this message as well.

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Passing information via segues
« Reply #1 on: June 16, 2014, 10:00:40 PM »
The problem is due to the fact that the label object hasn't been created yet at the point when prepareForSegue: is executed. If you set a breakpoint on this line:

Code: [Select]
viewController.label.text.name = name;
and then type the following in the Console panel:

Code: [Select]
po viewController.label
You will set that label is nil.

What you should do instead is:

1. Create a name property on the destination view controller.

2. In the prepareForSegue method, store the name value in that property.

3. In the viewDidLoad method of the ViewController class, take the value of the name property and store it into label.text.

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Passing information via segues
« Reply #2 on: June 16, 2014, 10:32:59 PM »
Thanks! It worked finally! All the books I searched through about the subject did not mention that subtle tidbit at all.