Why is my delegate method not being called?

2019-07-02 03:13发布

In this code I'm attempting to pass an array from one tab view to another using protocols. The method itself is a simple get method with one line returning an a mutableArray. Within it's own class it works, within this class it is not even called.

- (void)viewDidLoad
{
    [super viewDidLoad];
    myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]];
}

The header file for the class receiving the data:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol CoreDataDelegate;

@interface ListTableViewController : UITableViewController
{
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *myLocationEntityArray;

    id <CoreDataDelegate> delegate;
}

- (NSMutableArray *)fetchCoreData;

@property(nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property(nonatomic, assign) id <CoreDataDelegate> delegate;
@property(nonatomic, retain) NSMutableArray *myLocationEntityArray;

@end

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (NSMutableArray *) getMyLocationEntityArray;

@end

The top of the header file sending the data:

@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate>

1条回答
乱世女痞
2楼-- · 2019-07-02 04:12

First you should change your procotol like this :

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray;

@end

You set your MapViewController to responds to your protocol CoreDateDelegate. So, I suppose you alloc your ListTableViewController inside the MapViewController. If that is the case, you need to do this :

// MapViewController.m
...
ListTableViewController *listVC = [[ListTableViewController alloc] init];
listVC.delegate = self;
// display your listVC
...

// Somewhere in your code of MapViewController.m
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray {
   // do something with entityArray
}

EDIT

Following your comments, here is a simpler way to do what you want. NSNotification. It does not require protocol, and is easier to implement.

// In ListTableViewController.m
// In Init or viewDidLoad function


   [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getEntity:) 
                                                 name:@"GetEntity"
                                               object:nil];

- (void)getEntity:(NSNotification *)notif {
   NSArray *entityArray = (NSArray *)[notif object];
   // do something with entityArray
}

// In dealloc or viewDidUnLoad function
   [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:@"GetEntity"
                                               object:nil];


// In MapViewController.m
// theEntityArray to be defined
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GetEntity"
                                                         object:theEntityArray 
                                                       userInfo:nil];

In few words, when you will post the GetEntity notification in MapViewController, it will call undirectly the -(void)getEntity: function of ListTableViewController

查看更多
登录 后发表回答