2011-08-01 3 views
0

UIViewControllerUITableView이 있습니다. 이 뷰 컨트롤러에서는 인터넷에서 다운로드 한 일부 데이터를 표시하려고합니다. 그래서 이것을 위해 내가 할 예정이다 OfficesParser라는 도우미 클래스를 생성 한 다음Objective-C - NSObject 하위 클래스에서 UIViewController로 데이터 보내기

  1. 는 JSON 파서 ASIHTTPRequest
  2. 프로세스와 인터넷에서 데이터를 데이터를 다운로드하는 것은
  3. 완료
  4. 보내 내보기 컨트롤러에서 다시 내보기 컨트롤러

에 데이터 나는 alloc ING와 init 지금처럼 -viewDidLoad 내 도우미 클래스를 보내고 있어요 :

,
self.officesParser = [[[OfficesParser alloc] init] autorelease]; //officesParser is a retained property 

그런 다음 -viewWillAppear:에 나는 그렇게 같이 다운로드 프로세스를 시작합니다 officesParser 개체의 메서드를 호출 : 내 헬퍼 클래스에서

[self.officesParser download]; 

OfficesParserASIHTTPRequest는 때 큐를 알려주는 대리자 메서드가 있습니다 다운로드가 완료되었습니다. 그래서이 방법에서 내보기 컨트롤러에 데이터를 보내 주시기 바랍니다. 나는이 일을 할 생각 만하지 않았다 : 마음에이 코드

- (void)queueFinished:(ASINetworkQueue *)queue { 

    NSArray *offices = [self offices]; 
    OfficesViewController *ovc = [[OfficesViewController alloc] init]; 
    [ovc setOffices:offices]; 

} 

, 어떻게 내가 적절한 코드로 할 노력하고있어 달성 할 것인가?

답변

3

delegates and protocols을 살펴볼 필요가 있습니다. 클래스는 참조를 유지할 필요없이 클래스를 통신 할 수 있기 때문에 정확히 원하는 것입니다. Here이 이에 대한 또 다른 설명입니다.

+0

처럼 OfficesParser을 만들? –

+0

유지주기 사용 위임을 피하려면 이것이 쉬운 방법입니다. – Joe

+0

해킹이 아닙니다. 그것은 당신이하려는 일을 정확하게하기 위해 고안된 아키텍처 패턴입니다. 참조가 항상 최선의 방법은 아닙니다. – MishieMoo

0

귀하의 코드 :

OfficesViewController *ovc = [[OfficesViewController alloc] init]; 

OfficesViewController의 새로운 인스턴스 속성을 작성합니다. 새 인스턴스이므로 다운로드하고 구문 분석 한 후 트리거 한 OfficesViewController에 연결하지 않습니다. com/b/w OfficesViewControllerOfficesParser을에 대한 수정 된 방법으로 생성하여 OfficesViewController에 대한 주 포인터를 허용합니다.

@interface OfficesParser() 

@property(nonatomic,assign)OfficesViewController *ovc; 

@end 

@implementation OfficesParser 

@synthesize ovc; 

-(id)initWithDelegate:(OfficesViewController*)delegate{ 
    ovc = delegate; 
    return [self init]; 
} 

이제 ovc 대리인에 액세스 할 수 있습니다.

- (void)queueFinished:(ASINetworkQueue *)queue { 
    NSArray *offices = [self offices]; 
    [ovc setOffices:offices]; 
} 

마지막 그래서이 작업을 수행하는 쉬운 해킹/코드가 없습니다 그

self.officesParser = [[OfficesParser alloc] initWithDelegate: self]; 
+0

감사! 나는 이것을 시험 할 것이다! 속성을 retain과 함께 선언 할 필요가 없으며 실제로 코드에서 PVC 속성을 사용하고 있습니까? (적어도 나는 속성 호출을 볼 수 없다) –

+0

그것은 주 참조이므로 할당으로 선언되어있다. 'ovc'는'queueFinished' 메소드'[ovc setOffices : offices];에 사용됩니다. – Cyprian

+0

약한 참조를 사용하여 유지주기를 피합니다.더 많은 aobut 유지 사이클을 찾으려면이 답변을 읽어보십시오. [http://stackoverflow.com/questions/791322/retain-cycles-why-is-that-such-a-bad-thing/791372#791372](http:// stackoverflow.com/questions/791322/retain-cycles-why-is-that-such-a-bad-thing/791372#791372) – Cyprian

관련 문제