Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么没有调用我的委托方法?_Iphone_Objective C_Xcode_Delegates_Protocols - Fatal编程技术网

Iphone 为什么没有调用我的委托方法?

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]];

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

- (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委托是指向另一个视图控制器(或对象)的指针。如果将listVC.delegate=self放在didFinishLaunchingWithOptions中,这将意味着您的AppDelegate将是调用procotol函数的位置。是否也在AppDelegate中创建了MapViewController?如果是这种情况,您可以执行listVC.delegate=mapVC(其中mapVC是您的MapViewController实例)。因此,请按照我在回答中解释的那样使用协议函数。我希望MapViewController将MyLocationEntityArray发送到ListViewController。你的密码是那样的,不是吗?因为我看不出getMyLocationEntityArray方法有什么用处,除非它是在ListTableViewController中编写的,以便保存entityArray参数。