为什么我的委托方法没有被调用?
在此代码中,我尝试使用协议将数组从一个选项卡视图传递到另一个选项卡视图。该方法本身是一个简单的 get 方法,其中一行返回一个 mutableArray。在它自己的类中它可以工作,在这个类中它甚至没有被调用。
- (void)viewDidLoad
{
[super viewDidLoad];
myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]];
}
接收数据的类的头文件:
#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
发送数据的头文件的顶部:
@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该像这样更改您的协议:
您将
MapViewController
设置为响应您的协议CoreDateDelegate
。因此,我假设您在MapViewController
内分配了ListTableViewController
。如果是这种情况,您需要执行以下操作:编辑
根据您的评论,这里有一个更简单的方法来执行您想要的操作。
NSNotification
。它不需要协议,并且更容易实现。简而言之,当您在
MapViewController
中发布 GetEntity 通知时,它会间接调用ListTableViewController
的-(void)getEntity:
函数First you should change your procotol like this :
You set your
MapViewController
to responds to your protocolCoreDateDelegate
. So, I suppose you alloc yourListTableViewController
inside theMapViewController
. If that is the case, you need to do this :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 few words, when you will post the GetEntity notification in
MapViewController
, it will call undirectly the-(void)getEntity:
function ofListTableViewController