Iphone 为什么没有调用我的委托方法?
在这段代码中,我试图使用协议将数组从一个选项卡视图传递到另一个选项卡视图。该方法本身是一个简单的get方法,其中一行返回一个可变数组。在它自己的类中它工作,在这个类中它甚至没有被调用Iphone 为什么没有调用我的委托方法?,iphone,objective-c,xcode,delegates,protocols,Iphone,Objective C,Xcode,Delegates,Protocols,在这段代码中,我试图使用协议将数组从一个选项卡视图传递到另一个选项卡视图。该方法本身是一个简单的get方法,其中一行返回一个可变数组。在它自己的类中它工作,在这个类中它甚至没有被调用 - (void)viewDidLoad { [super viewDidLoad]; myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]];
- (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>
#导入
#进口
#进口
@协议代理;
@接口ListTableViewController:UITableViewController
{
NSManagedObjectContext*managedObjectContext;
NSMutableArray*myLocationEntityArray;
id代表;
}
-(NSMutableArray*)获取核心数据;
@属性(非原子,保留)NSManagedObjectContext*managedObjectContext;
@属性(非原子,赋值)id委托;
@属性(非原子,保留)NSMutableArray*myLocationEntityArray;
@结束
@协议CoreDataDelegate
//-(NSMutableArray*)获取核心数据;
-(NSMutableArray*)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>
@接口MapViewController:UIViewController
首先,您应该这样更改您的procotol:
@protocol CoreDataDelegate
//- (NSMutableArray *) fetchCoreData;
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray;
@end
将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
}
编辑
根据您的意见,这里有一个更简单的方法来做您想做的事情<代码>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];
简言之,当您在
MapViewController
中发布GetEntity通知时,它将直接调用-(void)GetEntity:
函数,该函数位于ListTableViewController
到达viewDidLoad
时是否已设置委托?我打赌是零。你在哪里分配代表?您确定self.delegate在请求位置实体数组时不是nil吗?如何设置它?我怀疑这可能是问题所在,于是尝试了self.delegate=self,但出现了一个错误,告诉我这两种类型不兼容。有几条建议:“delegate”不够描述性,尤其是当您有两种类型时。试试“codeDataDelegate”和“locManDelegate”,这样你就不会把自己搞糊涂了(或者有人试图在这方面帮助你)。此外,在viewDidLoad中,将嵌套的一行打断为3或4行。这使得它更容易阅读和理解,也更容易调试-您可以在任何子步骤后删除NSLog,以查看错误或为零。只需完成此操作。委托确实为空。这是我第一次使用代理。我应该将其设置为什么?我已经在应用程序委托中创建了ListTableViewController的实例,并将其放置在tabBarController中。我应该在didFinishLaunchingWithOptions方法中应用listVC.delegate=self还是如上所述创建一个新实例?还有,为什么我们要将所需的数据设置为参数而不是返回值?我不怀疑你的权利,但我想知道为什么。我想你误解了我的意思。id