2011-12-14 31 views
3

このコードでは、プロトコルを使用して1つのタブビューから別のタブビューに配列を渡そうとしています。メソッド自体は、1行がmutableArrayを返す単純な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> 
+0

-(void)getEntity:機能は、デリゲートは、すでに設定されてundirectly呼びますか?私はそれがないと賭ける。 – DarkDust

+0

デリゲートはどこに割り当てられますか?ロケーションエンティティ配列を求めるときにself.delegateがnilではないと確信していますか? – jrturton

+0

どうすれば設定できますか?私はこれが問題であるかもしれないと思って、self.delegate = selfを試みましたが、2つのタイプが互換性がないと言っているエラーを受けました。 – Deco

答えて

4

まず、あなたがこのようなあなたのprocotolを変更する必要があります。

@protocol CoreDataDelegate 

//- (NSMutableArray *) fetchCoreData; 
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray; 

@end 

あなたのMapViewControllerをあなたのプロトコルCoreDateDelegateに応答するように設定しました。だから、私はあなたのListTableViewControllerMapViewControllerの中に割り当てていると思います。その場合は、あなたがこれを実行する必要があります。

// 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 
} 

EDIT

あなたのコメントに続いて、ここにあなたが欲しいものを行うための簡単な方法です。 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通知をポストするとき、それはviewDidLoad`に達したときに `ListTableViewController

+0

私はすでに、アプリケーションデリゲート内でListTableViewControllerのインスタンスを作成し、tabBarController内に配置しました。 didFinishLaunchingWithOptionsメソッド内でlistVC.delegate = selfを適用するか、上記のように新しいインスタンスを作成する必要がありますか? – Deco

+1

また、私たちが返すものではなく、必要なデータを引数として設定するのはなぜですか?私はあなたの権利は間違いないが、なぜそれを理解したいのですか? – Deco

+0

私はあなたがデリゲートプロトコルを誤解していると思います。 id デリゲートは、別のビューコントローラ(またはオブジェクト)へのポインタです。 didFinishLaunchingWithOptionsの中にlistVC.delegate = selfを置くと、これはAppDelegateがあなたのprocotol関数が呼び出される場所になることを意味します。 – Niko

関連する問題