Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the delegate with a storyboard

I've been debating with this for a while now, hope you can help me.

I've been creating an app using storyboards mostly, I have a point where I popup a modal box to add a new record, popup works fine, the problem is dismissing it.

I've followed Apple's instructions on how to properly close modal boxes using delegates, and that works fine, except I need to add a navigation controller to my modal box, because the add process requires two steps (here fullscreen):

enter image description here

The problem lies in setting the delegate, so here are my two questions:

1- In my root view class (My Tab) is a delegate of the Add class (the modal), everything is set up right except this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAdd"]) {
        [[segue destinationViewController] setDelegate:self];

    }
}

The problem lies in that [segue destinationViewController] is returning the navigationcontroller and not the AddDrinkViewController class (see the storyboard). How do I get around this? If I remove the navigation controller altogether, the code works fine setting the appropriate delegate.

2- Is there any way to set the delegate by dragging the outlets in the storyboard?

Thanks!

like image 782
andresmax Avatar asked Nov 07 '11 19:11

andresmax


3 Answers

You're right, the destinationViewController will be a UINavigationController in this case. I wrote a category to handle this common situation:

// category .h file
@interface UIStoryboardSegue (NavControllerExtensions)
// Gets destinationViewCotroller. But if that controller 
// is a NavigationController, returns the nav controller's 
// top level view controller instead.
@property (readonly) id topLevelDestinationViewController;
@end

// category .m file
@implementation UIStoryboardSegue (NavControllerExtensions)
- (id)topLevelDestinationViewController
{
  id dest = self.destinationViewController;
  if ([dest isKindOfClass:[UINavigationController class]]) {
    UINavigationController* nav = dest;
    dest = nav.topViewController;
  }
  return dest;
}
@end

So now you can just do this in any of your prepareForSegue methods, and not need to worry about whether there even exists a NavigationController:

[[segue topLevelDestinationViewController] setDelegate:self]
// another example:
MyViewController *vc = segue.topLevelDestinationViewController;
vc.delegate = self; // etc.

To answer your second question, I couldn't find a way to set the delegate within IB.

like image 144
Mike Mertsock Avatar answered Nov 19 '22 07:11

Mike Mertsock


I found a shorter way in my case (same as yours):

AddDrinkViewController *controller=[[[segue destinationViewController]viewControllers]objectAtIndex:0];
like image 9
mudlee Avatar answered Nov 19 '22 07:11

mudlee


Basically you need to create an
Instance of UINavigationController and assign destinationViewController to it
and grab its topView controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAdd"]) {

    UINavigationController *navigationController = segue.destinationViewController;
    AddDrinkViewController *addDrinkcontroller = (AddDrinkViewController *)navigationController.topViewController;

    addDrinkcontroller.delegate = self;

   }
}
like image 5
bhavya kothari Avatar answered Nov 19 '22 06:11

bhavya kothari