为什么不调用委托方法?

时间:2022-02-19 01:40:48

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.

在这段代码中,我尝试使用协议将数组从一个选项卡视图传递到另一个选项卡视图。方法本身是一个简单的get方法,其中一行返回一个mutableArray。在它自己的类中它工作,在这个类中它甚至不被调用。

- (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 个解决方案

#1


4  

First you should change your procotol like this :

首先你应该像这样改变你的procotol:

@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设置为响应您的协议CoreDateDelegate。我假设你在MapViewController里面分配你的ListTableViewController。如果是这样,你需要这样做:

// 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.

在你的评论之后,这里有一个更简单的方法来做你想做的事情。NSNotification。它不需要协议,而且更容易实现。

// 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

简单地说,当你在MapViewController中发布GetEntity通知时,它将调用不直接的GetEntity: ListTableViewController的功能。

#1


4  

First you should change your procotol like this :

首先你应该像这样改变你的procotol:

@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设置为响应您的协议CoreDateDelegate。我假设你在MapViewController里面分配你的ListTableViewController。如果是这样,你需要这样做:

// 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.

在你的评论之后,这里有一个更简单的方法来做你想做的事情。NSNotification。它不需要协议,而且更容易实现。

// 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

简单地说,当你在MapViewController中发布GetEntity通知时,它将调用不直接的GetEntity: ListTableViewController的功能。