2012-02-27 2 views
0

이것이 iphone/ipad의 여러보기간에 데이터 또는 메시지를 전달하는 올바른 방법인지 궁금합니다.dismissModalViewController보기 컨트롤러간에 전달하는 메시지

두 개의 ViewController, FirstViewController 및 SecondViewController가 있습니다. 다음 두 가지 방법으로 설정 한 두 ViewController 모두에서 NSString * 메시지를 속성으로 사용합니다.

FirstViewController.h에서 SecondViewController.h 클래스를 가져옵니다. 사용자가

내 SecondViewController.h에서
-(IBAction)ShowSecondView 
{ 

    SeondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]]; 

    secondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    secondView.message = @"Presented from First View"; 

    [self presentModalViewController:secondView animated:YES]; 

    [secondView release]; 

} 

, 내가 클래스를 가져 오는 첫 번째보기의 버튼을 탭하면 내가 호출되는이 IBAction를이 FirstViewController.h 사용자가 버튼을 탭하면 내가 호출되는이 IBAction를이 두 번째보기는

-(IBAction)GoBack 
{ 

    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]]; 

    firstView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    firstView.message = @"Presented from Second View"; 

    [self presentModalViewController:firstView animated:YES]; 

    [firstView release]; 

} 

메시지는 성공적으로보기 사이를 통과하지만 난에 부모보기로 돌아가려면 현재 뷰 컨트롤러를 해제하기 위해 [self dismissModalViewController]를 사용하는 경우, 메시지가 전달되지 않습니다.

+0

내 제안이 대리자를 사용 중입니다 – Bonny

답변

0

모달 뷰 컨트롤러를 닫을 때 데이터가 전송되지 않습니다. 일부 프로토콜을 작성하고 뷰를 닫기 전에 위임 메커니즘을 사용하여 상위 뷰에 값을 되돌릴 수 있습니다.

더 많은 정보 GoBack을에있는 당신의 firstView의 alloc'd 두 번째 뷰 컨트롤러를 제시 한 첫 번째보기 컨트롤러가 아닌 Apple doc

Basics of protocol and delegate in Objective-C

0

참조하십시오. FirstViewController 클래스의 새 인스턴스입니다. 이 firstView 인스턴스를 만드는 대신 두 번째보기 컨트롤러를 단순히 닫으려고합니다. 그러나 두 번째보기 컨트롤러에서 첫 번째보기 컨트롤러에 대한 포인터를 만들어 데이터를 설정할 수도 있습니다. 첫 번째 뷰 컨트롤러

@synthesize firstView; 

:

-(IBAction)ShowSecondView { 
    SeondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]]; 
    secondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    secondView.message = @"Presented from First View"; 
    secondView.firstView = self; 
    [self presentModalViewController:secondView animated:YES]; 
    [secondView release]; 
} 

에서 두번째보기 컨트롤러의 구현에

#import "FirstViewController.h" 
FirstViewController *firstView; 
@property (retain, nonatomic) firstViewController *firstView; 

: 두 번째보기 컨트롤러의 헤더

두 번째보기 컨트롤러 :

,
-(IBAction)GoBack { 
    firstView.message = @"Presented from Second View"; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

은 BTW, 나는 종종 BTW

Notifications을 사용하여, 위의 코드는 바로 내 머리 나갔어 안된, viewcontrollers 사이에서 통신 할 수있는 다른 방법이 있습니다. 거기에 결함이 있으면 사과드립니다.

+0

"주기적으로 가져 오기"문제가 발생합니다. 어떤 방법 으로든 알림이나 대리자를 사용합니다. 시간 내 줘서 고마워. –

+0

"순환 임포트"문제가 발생하지 않습니다. 이 접근법에서는 첫 번째 ViewController가 두 번째를 가져 오지만 두 번째는 첫 번째 가져 오기를 수행하지 않습니다 (첫 번째 항목으로 돌아가려면 닫습니다). – ader

관련 문제