2012-03-16 3 views
1

보기의 속성을 반환하는 프로토콜을 선언하고 구현하는 방법은 무엇입니까? 예. mainView라는 뷰가 있는데 다른 뷰인 customView가 배열을 요청할 때 배열을 반환 할 수 있기를 원합니다. 내가하고있는 일은 mainView 구현 파일에서 returnTheArray 함수를 사용하여 프로토콜을 선언하고이 프로토콜을 채택하도록 customView를 설정했지만이 시점에서 멈추었다. 올바르게 작동하려면 어떻게해야합니까? 또는 이것을하기에 더 효과적이고 쉬운/올바른 방법이 있습니까? 감사.프로토콜을 통해 두보기 컨트롤러간에 데이터 전달

+0

어 .. UIViewController? – meggar

+0

예. UIViewController에서 UITableViewController로 – nemesis

+0

왜 프로토콜이 필요한지 알지 못한다. 뷰 컨트롤러에 서로 참조가 있으면 그냥 메서드를 추가 할 수있다. – meggar

답변

2

프로토콜은 함수/메소드 이름, 매개 변수 및 반환 값의 선언 일뿐입니다. 나에게 맞는 프로토콜은 여러 클래스에 의해 수행 될 때만 합리적이다. 개인적으로 개별적인 헤더 인 protocolName.h에서 선언하는 것을 선호한다.

프로토콜을 준수하는 모든 클래스는이 메소드를 구현해야합니다. 제 생각에는 그렇게 간단합니다.

AClass.h

@itnerface AClass : NSObject의 { // 일부 속성 } // @property 문 @end

AClass.m

#include "BClass.h" 

@implementation AClass 

//@synthesize statements; 

- (void) aFunctionFetchingTheArray { 

    BClass *bClass = [[BClass alloc] initWithSomething:kParameter]; 

    NSArray *anArray = [bClass returnTheArray]; 

    //Do something with it 

} 

@end 

MyProtocol.h

@protocol MyProtocol 

- (NSArray *) returnTheArray; 

@end 

BClass.h

#include "MyProtocol.h" 

@interface BClass <MyProtocol> { 
// some properties in interface 
} 
// some @property 
// some methods 
@end 

BClass.m

#include "BClass.h" //No need to include MyProtocol.h here too, in this case 

- (NSArray *) returnTheArray { 
return [NSArray arrayWithObjects:@"A", [NSNumber numberWithtInt:1], [UIColor clearColor], somethingElse, evenMore, nil]; 
} 

// more methods 

@end 

수정하십시오 나의 내가 놓친 또는 중요한 무언가를 맞춤법이 틀린 경우.

+0

대리자 메서드 (BClass가 준수하는 프로토콜의 메서드 인 )의 이름 규칙을 언급 할 가치가 있습니다. 이것 좀 봐 : http://cocoawithlove.com/2009/06/method-names-in-objective-c.html –

+0

고마워! 나는 이것을 염두에 둘 것이다! – nemesis

+0

initWithSomething은 무엇을 의미합니까? Bclass * bClass는 일부 비어있는 컨트롤러가 아닌 컨트롤러 B 클래스라고 AClass에 어떻게 알릴 수 있습니까? – nemesis

관련 문제