2012-06-18 2 views
2

UINavigationController을 사용하는 방법을 보여주는 자습서 응용 프로그램이 있습니다. 대부분 앱이 제대로 작동합니다.살아남은 메모리 경고

메모리 경고를 시뮬레이트 할 때 일부 데이터가 손실됩니다. UINavigationController에 두 개의 UIViewController이 있습니다. 첫 번째 UIViewController보기에 UIButton이 있고 사용자가 해당 페이지를 UIButton 터치하면 두 번째 UIViewController이 생성되고 네비게이션 스택이 먼저 UIViewController 개까지 푸시됩니다. 두 번째 UIViewController에서 UIViewController까지 NSNotificationCenter을 통해 데이터를 전달합니다.

이 접근 방식을 사용하면 앱이 정상적으로 작동하지만 두 번째 메모리가 표시되면 메모리 경고가 표시되면 UIViewController의보기가 표시되지 않습니다. 그렇다면 어떻게 살아남을 수 있을까요?

+0

우리는 관련 코드를 볼 수 있습니다 : 그것은 처음의 UIViewController 객체

// // SecondViewController.h // test // // #import <UIKit/UIKit.h> #import "ViewController.h" @interface SecondViewController : UIViewController @property(nonatomic, retain) ViewController *parentViewController; @end 

과하는 .m 파일

// // SecondViewController.m // test // // #import "SecondViewController.h" @interface SecondViewController() -(IBAction)buttonPressed:(id)sender; @end @implementation SecondViewController @synthesize parentViewController; -(IBAction)buttonPressed:(id)sender { parentViewController.yourObject = @"your value"; } -(void)dealloc { [parentViewController release]; [super dealloc]; } 

두 번째의 ViewController 이렇게 밀어에 대한 참조를 유지? –

답변

0

메모리 경고가 트리거되면 응용 프로그램은 더 이상 필요하지 않은 모든 개체를 제거하려고 시도합니다. 아마 첫 번째 UIViewController에서 리스너가 제거됩니다.

NSNotificationCenter의 문제점은 리스너가 활성화되어 있는지 확인하는 쉬운 방법이 없다는 것입니다.

이 상황이 NSNotification 설정을 사용하기에 적합한 지 여부는 알 수 없습니다. 어떤 뷰 컨트롤러에 어떤 메시지를 보낼지 제어하기가 쉽지 않습니다.

아마도이 설정이 쉽고 (아마도 메모리가 더 안전 할 수 있습니다.)

SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    vc.parentViewController = self; 
    [self.navigationController pushViewController:vc animated:YES]; 
[vc release]; 
+0

답변 해 주셔서 감사합니다. 하지만 뷰 컨트롤러를 별도로 유지하는 것이 더 좋은 디자인이라고 생각합니다. 나는 데이터 변경을 유지하는 별도의 모델 클래스를 만들 것이라고 생각한다. – fyodor