quijote
quijote

Reputation: 37

prepareForSegue:sender: fails to alter property on destination VC

I am not sure what I am doing wrong. I have two modes -- edit and add. From VC1, when add button is touched it segues to VC2 and the user can fill out details and add an entry into a table of the the VC1. This works. However, I have someProblamaticUILabel in VC2 that I wish to display "Add Mode." I try to set it in in the segue but this does not work.

If the user then touches the table cell they created in VC1, I want the same VC2 to be used but I want to change the someProblamaticUILabel to read "Edit Mode." This does not work.

I have these wired up using the storyboard.

Below is some choice output

  From Segue of VC1
     segue.destinationViewController: AppVC2: <AppVC2: 0x146b5f40>
     AppVC2.someProblamaticUILabel: (null)
     AppVC2.someProblamaticUILabel.text: (null)
  From viewDidLoad of VC2: <AppVC2: 0x146b5f40>
     AppVC2 someFunctionalAttribute: edit
     AppVC2 someProblamaticUILabel: <UILabel: 0x1454efc0; frame = (16 20; 151 54); text = 'Add/Edit'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1454ef50>>
     AppVC2 someProblamaticUILabel.text: Add/Edit

Some code

//VC1

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"AddHomePlayerSegue" ]){
        AppVC2 * addHome = (AppVC2 *) segue.destinationViewController;
        [addHome setDelegate:self];
        addHome.teamType = HOME_TEAM;
        addHome.mode = ADD_MODE;
        addHome.someProblamaticUILabel.text = @"Add";

    }else if([segue.identifier isEqualToString:@"EditVisitorPlayerSegue" ]){
        AppVC2 * editVisitor = (AppVC2 *) segue.destinationViewController;
        [editVisitor setDelegate:self];
        editVisitor.mode = EDIT_MODE;
        editVisitor.someProblamaticUILabel.text = @"Edit";

        NSLog(@"\n  From Segue of VC1");
        NSLog(@"\n     segue.destinationViewController: AppVC2: %@", editVisitor);
        NSLog(@"\n     AppVC2.someProblamaticUILabel: %@", editVisitor.someProblamaticUILabel);
        NSLog(@"\n     AppVC2.someProblamaticUILabel.text: %@", editVisitor.someProblamaticUILabel.text);
    }
}

// VC2

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"\n  From viewDidLoad of VC2: %@", self);
    NSLog(@"\n     AppVC2 someFunctionalAttribute: %@", self.mode);
    NSLog(@"\n     AppVC2 someProblamaticUILabel: %@", self.someProblamaticUILabel);
    NSLog(@"\n     AppVC2 someProblamaticUILabel.text: %@", self.someProblamaticUILabel.text);
}

One more question What is the best way to tell VC2 that the user tapped x row with y data to be populated from VC1 to VC2?

Upvotes: 0

Views: 98

Answers (2)

Henit Nathwani
Henit Nathwani

Reputation: 442

You should check the sequence of methods, in which they are called.

Firstly, there will be

[self performSegueWithIdentifier:@"SEGUE_IDENTIFIER"];

Secondly,the viewDidLoad of your destination view controller will get called.

Thirdly, prepareForSegue in your first view controller will get called up.

Instead of checking the properties in viewDidLoad of destination view controller, try to log them in viewDidAppear of your destination view controller.

Hope this helps..

Upvotes: 0

rdurand
rdurand

Reputation: 7410

Your issue probably comes from the fact that when you try to set the text property on your UILabel, it has not been inited yet. The prepareForSegue: method is called before the transition, so before anything is loaded from your storyboard, and before the call of viewDidLoad on your VC2.

Try this :

• Remove the two lines addHome.someProblamaticUILabel.text = @"....."; from your prepareForSegue: method.

• In VC2, in the method viewDidLoad, test self.mode and set the label according to this value :

-(void)viewDidLoad {
    [super viewDidLoad];

    if ([self.mode isEqualToString:ADD_MODE]) self.someProblamaticUILabel.text = @"Add";
    else if ([self.mode isEqualToString:EDIT_MODE]) self.someProblamaticUILabel.text = @"Edit";
    else self.someProblamaticUILabel.text = @"Unknown";
}

Upvotes: 1

Related Questions