2013-01-23 2 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

문제의 코드가 잘 포맷되지 않아 (많은 빈 줄) 읽고 읽기가 어렵습니다. 첫 번째 추측은 두 번째로 대리인을 설정할 때 첫 번째 설정을 재정의한다는 것입니다. 한 대리인 ivar는 하나의 참조 만 보유 할 수 있습니다. –

+0

@Andrey - 음 ... 당신이 옳다고 생각합니다. setDelegate가 무시되고 있습니다. 그러한 경우, 동시에 2 개 이상의 다른 viewController에서 위임 메서드를 호출 할 수 있습니다. 내 이해 - 그 viewController 대리자가 될 구현해야합니다 것들 중 하나는 setDelegate. ???? ???? 말이 돼 ?? – pete

+0

@pete 나에게 로크 야르에게 묻고 있니? –

답변

1

v2에는 "print"메서드가 없습니다. 이는 v3의 프로토콜 메서드입니다. 이와 같은 메시지를 연결할 수 없습니다. 여러 컨트롤러가 다른 컨트롤러의 항목에 응답하게하려면 NSNotification을 사용해야합니다. 모든 수의 개체를 등록하여 알림을받을 수 있습니다.

+0

대단히 감사합니다. 귀하의 대답은 매우 정확하고 핵심입니다. 이제 NSNotification에 대해 모두 배우기로 넘어갈 수 있습니다. 다시 한번 감사드립니다. – pete