Using a delegate to communicate between a UIPopoverViewController and a MainViewController
I am working on an iPad app that has a main view (In this case an MKMapView) with a PopoverView controller which contains a UITableViewController. The content in the UITableview contains a number of variable elements which I need for dropping annotation pins on the MapView. These variables change depending on which row is selected. My thought was to use a delegate to accomplish this but I am struggling to implement it. 

I have declared delegate in the showIncidentList method (see enclosed code).  My questions are, is this custom delegate appropriate for this desired outcome, and if so, what am i missing that will allow me to communicate the information in the delegate and call the plotCallLocations method on the main view with the updated information from the delegate.

Relevent code:

**SearchViewController.h**  //the popoverViewController class

   

    @protocol IncidentPickerDelegate 

    - (void)incidentSelected:(NSMutableArray *)incidentDetail;

    @end

    @interface SearchViewController : UITableViewController  <UITableViewDelegate, UITableViewDataSource> {	
    __weak id<IncidentPickerDelegate> _delegate;
    }

    @property (nonatomic, retain) NSMutableArray *incidentDetails;
    @property (nonatomic, weak) id<IncidentPickerDelegate> delegate;
    @end

**SearchViewController.m**

    #import "TwitterSearchViewController.h"
    #import "CallViewController.h"

    @implementation SearchViewController

    @synthesize delegate = _delegate;
    @synthesize incidentDetails= _incidentDetails;

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    _incidentDetails = [[NSMutableArray alloc] initWithObjects:[textItems objectAtIndex:0], [textItems objectAtIndex:1], lat, lon, nil];     //textItems is an NSArray of parsed JSON data, lat and lon are int's
            
            if (_delegate !=nil) {
                NSMutableArray *incidentDet = _incidentDetails;
                [_delegate incidentSelected:incidentDet]; 
    
    @end

**CallViewController.h**    //MainViewController

    #import "SearchViewController.h"

    @interface CallViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UIAlertViewDelegate, IncidentPickerDelegate> {
    }

    @property (nonatomic, retain) UIPopoverController *incidnetListPopover;
    @property (nonatomic, retain) SearchViewController *incidentPicker;

    -(IBAction)showIncidentList:(id)sender;

**CallViewController.m**

    #import "CallViewController.h"
    #import "SearchViewController.h"

    @implementation CallViewController

    @synthesize incidnetListPopover = _incidnetListPopover;
    @synthesize incidentPicker = _incidentPicker;

    - (IBAction)showIncidentList:(id)sender {
    if (_incidentPicker == nil) {
        self.incidentPicker = [[TwitterSearchViewController alloc] 
                             initWithStyle:UITableViewStylePlain];
        _incidentPicker.delegate = self;
        
        self.incidnetListPopover = [[UIPopoverController alloc] 
                                    initWithContentViewController:_incidentPicker];               
    }
    
    [self.incidnetListPopover presentPopoverFromBarButtonItem:sender 
                                    permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    }

    - (void)plotCallLocations:(NSMutableArray *)incidentDetail {
    
    
    //    for (id<MKAnnotation> annotation in _mapView.annotations) {
    //      [_mapView removeAnnotation:annotation];
    
    NSLog(@"plotCall called");
    NSNumber * latitude = [incidentDetail objectAtIndex:2];
    NSNumber * longitude = [incidentDetail objectAtIndex:3];
    NSString * name = [incidentDetail objectAtIndex:1];
    NSString * address = [incidentDetail objectAtIndex:0];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;  
    
    CallLocation *annotation = [[CallLocation alloc] initWithName:name address:address coordinate:coordinate];
    [_mapView addAnnotation:annotation];  
    
    [self.incidnetListPopover dismissPopoverAnimated:YES];
    
    }

Using a delegate to communicate between a UIPopoverViewController and a MainViewController
===

I am working on an iPad app that has a main view (In this case an MKMapView) with a PopoverView controller which contains a UITableViewController. The content in the UITableview contains a number of variable elements which I need for dropping annotation pins on the MapView. These variables change depending on which row is selected. My thought was to use a delegate to accomplish this but I am struggling to implement it. 

I have declared delegate in the showIncidentList method (see enclosed code).  My questions are, is this custom delegate appropriate for this desired outcome, and if so, what am i missing that will allow me to communicate the information in the delegate and call the plotCallLocations method on the main view with the updated information from the delegate.

Relevent code:

**SearchViewController.h**  //the popoverViewController class

   

    @protocol IncidentPickerDelegate 

    - (void)incidentSelected:(NSMutableArray *)incidentDetail;

    @end

    @interface SearchViewController : UITableViewController  <UITableViewDelegate, UITableViewDataSource> {	
    __weak id<IncidentPickerDelegate> _delegate;
    }

    @property (nonatomic, retain) NSMutableArray *incidentDetails;
    @property (nonatomic, weak) id<IncidentPickerDelegate> delegate;
    @end

**SearchViewController.m**

    #import "TwitterSearchViewController.h"
    #import "CallViewController.h"

    @implementation SearchViewController

    @synthesize delegate = _delegate;
    @synthesize incidentDetails= _incidentDetails;

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    _incidentDetails = [[NSMutableArray alloc] initWithObjects:[textItems objectAtIndex:0], [textItems objectAtIndex:1], lat, lon, nil];     //textItems is an NSArray of parsed JSON data, lat and lon are int's
            
            if (_delegate !=nil) {
                NSMutableArray *incidentDet = _incidentDetails;
                [_delegate incidentSelected:incidentDet]; 
    
    @end

**CallViewController.h**    //MainViewController

    #import "SearchViewController.h"

    @interface CallViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UIAlertViewDelegate, IncidentPickerDelegate> {
    }

    @property (nonatomic, retain) UIPopoverController *incidnetListPopover;
    @property (nonatomic, retain) SearchViewController *incidentPicker;

    -(IBAction)showIncidentList:(id)sender;

**CallViewController.m**

    #import "CallViewController.h"
    #import "SearchViewController.h"

    @implementation CallViewController

    @synthesize incidnetListPopover = _incidnetListPopover;
    @synthesize incidentPicker = _incidentPicker;

    - (IBAction)showIncidentList:(id)sender {
    if (_incidentPicker == nil) {
        self.incidentPicker = [[TwitterSearchViewController alloc] 
                             initWithStyle:UITableViewStylePlain];
        _incidentPicker.delegate = self;
        
        self.incidnetListPopover = [[UIPopoverController alloc] 
                                    initWithContentViewController:_incidentPicker];               
    }
    
    [self.incidnetListPopover presentPopoverFromBarButtonItem:sender 
                                    permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    }

    - (void)plotCallLocations:(NSMutableArray *)incidentDetail {
    
    
    //    for (id<MKAnnotation> annotation in _mapView.annotations) {
    //      [_mapView removeAnnotation:annotation];
    
    NSLog(@"plotCall called");
    NSNumber * latitude = [incidentDetail objectAtIndex:2];
    NSNumber * longitude = [incidentDetail objectAtIndex:3];
    NSString * name = [incidentDetail objectAtIndex:1];
    NSString * address = [incidentDetail objectAtIndex:0];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;  
    
    CallLocation *annotation = [[CallLocation alloc] initWithName:name address:address coordinate:coordinate];
    [_mapView addAnnotation:annotation];  
    
    [self.incidnetListPopover dismissPopoverAnimated:YES];
    
    }
