2013-01-23 18 views
0

所以我有3個視圖,如下所示:viewController >> viewController2 >> viewController3。 在viewController3中,我創建了一個委託協議。協議方法是一個打印出NSLog的簡單方法。具有三個viewControllers的代表

當我從ViewController3調用委託時,只有它的父級(viewController2)不響應(第一個)viewController。沒有錯誤。我認爲問題與[v2 setDelegate:self]有關;在viewController.m文件中。不過,[self.v3 setDelegate:self];在ViewController2.m文件中工作正常。

爲什麼(第一個)viewController委託沒有響應?代表只與直接的孩子一起工作嗎?

> **ViewController.h** 
    #import <UIKit/UIKit.h> 
    #import "ViewController2.h" 
    #import "ViewController2.h" 
    @interface ViewController : UIViewController <PassData>{ 

    ViewController2 *v2; 
    } 
    @property (strong, nonatomic) ViewController2 *v2; 

> Blockquote 

    - (IBAction)button:(id)sender; 
    @end 

> **ViewController.M** 

    #import "ViewController.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController 
    @synthesize v2; 

    - (IBAction)button:(id)sender { 

    v2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil]; 
    [v2 setDelegate:self]; 
    [self.view addSubview:v2.view]; 
    } 

    -(void)print: (BOOL)success;{ 

    if (success == YES) { 
     NSLog(@"ViewController called"); 
    } 

    } 

    @end 

> > ViewController2.h 

    #import <UIKit/UIKit.h> 
    #import "ViewController3.h" 

    @interface ViewController2 : UIViewController <PassData> { 

    ViewController3 *v3; 

    } 
    @property (strong, nonatomic)ViewController3 *v3; 
    @property (retain) id delegate; 

    - (IBAction)button:(id)sender; 
    @end 

    ViewController2.m 

    #import "ViewController2.h" 

    @interface ViewController2() 

    @end 

    @implementation ViewController2 
    @synthesize v3,delegate; 

    - (IBAction)button:(id)sender { 

    v3 = [[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil]; 
    [self.v3 setDelegate:self]; 
    [self.view addSubview:v3.view]; 

    } 

    -(void)print: (BOOL)success;{ 

    if (success == YES) { 
      NSLog(@"ViewController2 called"); 
    } 
    } 
    @end 

> ViewController3.h 

    #import <UIKit/UIKit.h> 

    @protocol PassData <NSObject> 

    @required 

    -(void)print:(BOOL)success; 

    @end 

    @interface ViewController3 : UIViewController { 

    id<PassData> delegate; 

    } 

    @property (retain) id delegate; 

    - (IBAction)callButton:(id)sender; 

    @end 

    ViewController3.m 

    #import "ViewController3.h" 

    @interface ViewController3() 

    @end 

    @implementation ViewController3 
    @synthesize delegate; 

    - (IBAction)callButton:(id)sender { 

    // call all delegates 
     [[self delegate]print:YES]; 

    } 
    @end 
+0

您的問題中的代碼格式不正確(很多空行)並且難以閱讀。首先猜測的是,當您第二次設置委託時,您會覆蓋第一個設置。一位代表伊娃可以只有一個參考。 –

+0

@Andrey - 呃...我認爲你是對的 - setDelegate被覆蓋。如果是這種情況,那麼委託方法如何在同一時間在兩個或更多不同的viewControllers中被調用。我的理解是 - 對於viewController是一個委託,它必須實現的一件事是setDelegate。那有意義嗎 ?? – pete

+0

@pete你問我還是rokjarc? –

回答

1

v2沒有「打印」方法,這是v3的協議方法 - 您不能像這樣鏈接委託消息。如果您希望多個控制器響應另一個控制器中的某些內容,則應該使用NSNotification - 可以註冊任意數量的對象以接收通知。

+0

非常感謝。你的回答非常精確和直截了當。現在我可以繼續學習關於NSNotification的所有內容。再次感謝。 – pete