为什么我的委托方法不被调用? [英] Why is my delegate method not being called?
问题描述
在这段代码中,我尝试使用协议将数组从一个选项卡视图传递给另一个。该方法本身是一个简单的get方法,一行返回一个mutableArray。在它自己的类里面,它在这个类里面甚至不被调用。
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
发送数据的头文件的顶部: / p>
The top of the header file sending the data:
@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate>
推荐答案
首先你应该像这样改变你的procotol: p>
First you should change your procotol like this :
@protocol CoreDataDelegate
//- (NSMutableArray *) fetchCoreData;
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray;
@end
您设置了 MapViewController
来响应你的协议 CoreDateDelegate
。所以,我想你在 MapViewController
中分配你的 ListTableViewController
。如果是这样,你需要这样做:
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
}
em>
按照你的意见,这里是一个更简单的方法来做你想要的。 NSNotification
。它不需要协议,更容易实现。
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];
几句话,当您将发布GetEntity通知在 MapViewController
,它将不定期调用 - (void)getEntity:
函数 ListTableViewController
In few words, when you will post the GetEntity notification in MapViewController
, it will call undirectly the -(void)getEntity:
function of ListTableViewController
这篇关于为什么我的委托方法不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!