Lander Iphone
Lander Iphone

Reputation: 47

segues in storyboard work perfectly but the prepare for segue is not being called

So I have a standard method that everyone uses in the prepare for segue. When I think logically "prepare" means do this before the segue is called. But I have set up 3 table cells that all have 1 segue to the SecondViewController. When I test that it works perfectly no errors.

Now when I want to add to this app that when the user selects the first cell the label "labeltje" get another text value this does not happen. Of course this code is in my TableViewController.m file and not in the DagenViewController, which is my second controller.

What am I not seeing? Dagenviewcontroller is imported etc. no issues there. This is the prepareforsegue bit of code:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DagenViewController *secondVC = (DagenViewController *)segue.destinationViewController;
    if([segue.identifier isEqualToString:@"vrijdagSegue"])
    {
        NSString *vrijdag = @"vrijdag";
        secondVC.labeltje.text = vrijdag;
    }
    else if([segue.identifier isEqualToString:@"zaterdagSegue"])
    {
        NSString *zaterdag = @"zaterdag";
        secondVC.labeltje.text = zaterdag;
    }
    else {
        NSString *zondag = @"zondag";
        secondVC.labeltje.text = zondag;
    }
}

Or is there a way to put something in the ViewDidLoad method of my SecondViewController that checks which segue was used?

Thank you for reading.

Upvotes: 0

Views: 122

Answers (2)

CRDave
CRDave

Reputation: 9285

Try to do this way:

DagenViewController.h file

@property (strong, strong) NSString *preText

DagenViewController.m file

- (void) viewDidLoad
{
    self.labeltje.text = preText;
}

In Your Method

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DagenViewController *secondVC = (DagenViewController *)segue.destinationViewController;
    if([segue.identifier isEqualToString:@"vrijdagSegue"])
    {
        secondVC.preText = @"vrijdag";
    }
    else if([segue.identifier isEqualToString:@"zaterdagSegue"])
    {
         secondVC.preText = @"zaterdag";
    }
    else {
         secondVC.preText = @"zondag";
    }
}

Upvotes: 1

e1985
e1985

Reputation: 6279

When prepareForSegue:sender: is called you are accessing a label that is nil since the view controller's view was not yet loaded. Create a string property(that you will set in prepareForSegue:sender:) and then in your DagenViewController viewDidLoad method set the label's text from that string property.

Upvotes: 1

Related Questions