How to properly use modal view controller with the xcode 4.2 storyboard

17,626

Take a look at this tutorial

According to it, you should set the delegate as follows:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"])
    {
        UINavigationController *navigationController = 
          segue.destinationViewController;
        PlayerDetailsViewController 
          *playerDetailsViewController = 
            [[navigationController viewControllers] 
              objectAtIndex:0];
        playerDetailsViewController.delegate = self;
    }
}

Where @"AddPlayer" is the name of your 'modal' segue

Share:
17,626
RDM
Author by

RDM

Updated on June 08, 2022

Comments

  • RDM
    RDM almost 2 years

    I was wondering how to properly use the storyboard to put up a view controller modally. Personally I prefer working with xibs, but it seems that the storyboard is gaining popularity and will be the way to go in the future.

    The way I would normally put up a view controller modally would be like this: let's say we have ViewControllerA (A for short) and ViewControllerB (B for short). I would then normally put a protocol in B.h specifying the delegate method when B wants to be dismissed and add the id<theProtocol> delegate field as an assign property. Assuming i'm busy in A and I want to present B modally, I would write:

    B* b = [[B alloc] initWithNibName:@"B" bundle:nil];
    b.delegate = self;
    [self presentModalViewController:B animated:YES];
    

    Using the storyboard, I know it's possible to put up a different view controller in a modal way by ctrl-dragging from a button to a viewcontroller and selecting modal as transition type. I'm just wondering though; where do I set the delegate of the new view controller? What's the correct practice of passing things to your modal view controller? I don't really know what the whole deal with Segues is...

  • RDM
    RDM over 12 years
    I looked at the tutorial and obviously I have no benefit in doubting your reply, but wouldn't you agree that this is much uglier code than the before? It just seems like such a hassle to do something that was really easy before...
  • d.lebedev
    d.lebedev over 12 years
    Don't use storyboards if it seems ugly for you
  • RDM
    RDM over 12 years
    That's the main reason I don't use them now, I'm just "afraid" they'll become the main way to do it in the future.
  • Besi
    Besi over 12 years
    @d.lebedev I think Storyboards have many advantages but also some disadvantages, you might want to checkout my reply: stackoverflow.com/questions/8495179/…
  • Jai Srivastav
    Jai Srivastav almost 12 years
    Oh wait. The previous answer did this but they assigned the destination view controller to a navigation view controller which you do not have to do (I don't even know if that works...).