Calling method to view controller delegate, won't dismiss modal view

744 Views Asked by At

I have the following simple view controller class set up

@protocol ThermoFluidsSelectorViewControllerDelegate;

@interface ThermoFluidsSelectorViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) id <ThermoFluidsSelectorViewControllerDelegate> delegate;
// user hits done button
- (IBAction)done:(id)sender;
@end

@protocol ThermoFluidsSelectorViewControllerDelegate <NSObject>
-(void) didFinishSelection:(ThermoFluidsSelectorViewController *)controller fluidID:    (NSString *)fluidID;
@end

the 'didFinishSeletion: fluidID:' method is defined in the master view controller and should dismiss the selector view controller when called. When the done button is pressed the following method is called:

- (IBAction)done:(id)sender
{
    [[self delegate] didFinishSelection:self fluidID:nil];
}

the 'done:' method gets called (checked with an alert) but 'didFinishSelection...' is not getting called so the view will not revert back to the main screen. Any ideas?

1

There are 1 best solutions below

1
On BEST ANSWER

It sounds like you have not assigned your delegate in your master view controller.

You should have something like this in your master view controller which sets up the delegate:

    ThermoFluidsSelectorViewController *view = [[ThermoFluidsSelectorViewController alloc] init];
    view.delegate = self;

here you can see I create the view, then set the delegate of the view back to myself.

If you are not creating the Thermo... view controller programatically, but have used a storyboard, then you can set the delegate in the prepareForSegue: method of your master view controller:

// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure we're referring to the correct segue
    if ([[segue identifier] isEqualToString:@"MySegueID"]) {

    // Get reference to the destination view controller
    ThermoFluidsSelectorViewController *cont = [segue destinationViewController];

    // set the delegate
    cont.delegate = self;

Hope this helps.