2011-12-14 315 views
3

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]]; 
} 

该类接收数据头文件:

@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate> 
+0

-(void)getEntity:功能'达到viewDidLoad'时已经设置委托?我敢打赌它是零。 – DarkDust

+0

你在哪里分配代表?你确定self.delegate不是零,当你问它的位置实体数组? – jrturton

+0

我该如何设置?我怀疑这可能是问题,并试图self.delegate =自我,但有一个错误告诉我这两种类型是不兼容的。 – Deco

回答

4

首先,你应该改变你的网络协议是这样的:

#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 

头文件发送数据的顶部:

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

在几句话,当你将发布GetEntity通知,MapViewController,它会undirectly调用ListTableViewController

+0

我已经在应用程序委托中创建了ListTableViewController的一个实例,并将它放在tabBarController中。我应该在didFinishLaunchingWithOptions方法中应用listVC.delegate = self还是像上面那样创建一个新实例? – Deco

+1

另外,为什么我们要将我们想要的数据作为参数而不是我们的回报?我毫不怀疑你的权利,但我想明白为什么。 – Deco

+0

我认为你误解了委托协议。 id 委托是指向另一个视图控制器(或对象)的指针。如果你把listVC.delegate = self放在didFinishLaunchingWithOptions中,这将意味着你的AppDelegate将是你的协议函数被调用的位置。 – Niko

相关问题