user-44651
user-44651

Reputation: 4124

Unwinding from Storyboard prevents Delegate from being called

I have a UIViewController several segues deep that when the use is finished, should unwind and take them back to the DashboardViewController.

I created the unwindToDashboard method in the Dashboard and hooked a button up to the Exit in my FinishViewController. So that when its clicked will fire the unwind action.

That works fine.

But I need to pass back data to the Dashboard from the FinishViewController.

So I created a delegate ProcessDataDelegate for the FinishViewController and made the Dashboard conform to it.

However, the delegate method in the Dashboard is NOT called.

Can anyone tell me what I have done wrong?

DashboardViewController.m

#import "FinishViewController.h"

@interface DashboardViewController () <ProcessDataDelegate>{
    FinishViewController *fm;
}
@end

@implementation DashboardViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if(!fm){
        fm = [[FinishViewController alloc] init];

    }

    fm.delegate = self;
}

- (IBAction)unwindToDashboard:(UIStoryboardSegue *)unwindSegue {
//empty
}

#pragma mark PROTOCOL METHODS

-(void) didFinishWithResults:(NSDictionary*) dictionary{
    NSLog(@"Dashboard method called didFinishWithResults");
}

@end

FinishViewController.h

@class FinishViewController;
@protocol ProcessDataDelegate <NSObject>
    @required
    - (void) didFinishWithResults: (NSDictionary*)dictionary;
@end

@interface FinishViewController : UIViewController 
@property (nonatomic, weak) id <ProcessDataDelegate>  delegate;
@end

FinishViewController.m

@interface FinishViewController () 
@end

@implementation FinishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"fired via the button to the exit--- Segue: %@", [segue identifier]);
    [delegate didFinishWithResults:nil ];
}

@end

Upvotes: 1

Views: 49

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

You need pass your delegate in prepareForSegue method of your DashboardViewController, there get the destinationViewController and cast to FinishViewController if the identifier is equal to your expected segue identifier

Something like this

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"fired via the button to the exit--- Segue: %@", [segue identifier]);
    if([[segue identifier] isEqualToString:@"YourSegueIdentifier"])
    {
         ((FinishViewController*)[segue destinationViewController]).delegate = self
    }
}

Upvotes: 1

Related Questions