views:

39

answers:

3

I have a modal view which gets the user to select some data to add to a table. When the user presses a save button, the modal view should disappear and send the required data back to the view controller that presented the modal view for further processing. To achieve this, I have set up a protocol. The protocol method in the original view controller does not get called. My code is below, what am I doing wrong?

The header file (modal view controller):

@protocol AddTAFDataSource;

@interface AddTAFViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
    id<AddTAFDataSource> dataSource;
    NSString *newICAOCode;
}

@property (nonatomic, assign) id<AddTAFDataSource> dataSource;

- (IBAction)saveButtonPressed;

@end

@protocol AddTAFDataSource <NSObject>
- (void)addNewTAF:(AddTAFViewController *)addTAFViewController icao:(NSString *)icaoCode;
@end

The implementation file (modal view controller):

#import "AddTAFViewController.h"
#import "TAFandMETARViewController.h"

@implementation AddTAFViewController

@synthesize dataSource;
...
- (IBAction)saveButtonPressed {
    [self.dataSource addNewTAF: self icao: newICAOCode]; 
}

@end

Presenting view controller header file:

#import "AddTAFViewController.h"

@interface TAFandMETARViewController : UITableViewController <AddTAFDataSource> {

}

@end

And finally, the presenting view controller:

#import "AddTAFViewController.h"  
        ...  
- (void)insertNewObject:(id)sender {
        AddTAFViewController *addTAFViewController = [[AddTAFViewController alloc] initWithNibName: @"AddTAF" bundle: [NSBundle mainBundle]];
        addTAFViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [(AddTAFViewController *)self.view setDataSource: self];
        [self presentModalViewController: addTAFViewController animated: YES];
        addTAFViewController = nil;
        [addTAFViewController release];
}

- (void)addNewTAF:(AddTAFViewController *)addTAFViewController icao:(NSString *)icaoCode {
        newICAO = icaoCode;
        [self dismissModalViewControllerAnimated: YES];
}

Just to remind, it is the above -(void)addNewTAF: method that does not get messaged. Any help/pointers in the right direction are much appreciated.

+2  A: 

Replace:

[(AddTAFViewController *)self.view setDataSource: self];

With:

[addTAFViewController setDataSource:self]

After all, the dataSource is a property of the controller, not a controller's view.

imaginaryboy
Thanks. I was copying something I had seen before (as I struggle to get my head round protocols, however, I'm getting there), which was a property of a view. I guess it makes sense when you take a step back and look at it. Thanks again, that's even more hours of sweat and blood saved!
churchill614
A: 

You are wrong at this point:

[(AddTAFViewController *)self.view setDataSource: self];

you should write this instead:

addTAFViewController.dataSource = self;
eviltrue
A: 

Rather than trying to use a separate object (your dataSource) to pass data between the two view controllers, you could simply use add properties to contain the data directly in the view controller you're going to present modally (here, the AddTAFViewController).

Then in the method you use to dismiss the modal view controller, before dismissing it you can send [self modalViewController] to get the modal view controller, and at that point the parent view controller can send it any messages it wants. That would allow you to grab whatever data you need from the modal view controller, so you wouldn't need the data source and the protocol at all.

jlehr