2012-07-17 5 views
0

단일보기가있는 간단한 응용 프로그램이 있습니다. 버튼이 있습니다. 버튼을 클릭하면 두 번째보기가 추가됩니다. 이 두 번째보기에는 하나의 UIBarButtonItem이있는 간단한 도구 모음이 있습니다. 내 View Controller의 메시지를 시작하기 위해 등록되어 있습니다.ARC에서 내보기 컨트롤러보기를 해제합니다.

하지만 버튼을 클릭하자마자 앱이 다운됩니다. 좀비를 사용하도록 설정하면 내 View Controller가 해고당했습니다. 함수를 추가하고 NSLog()을 호출하면 내보기가 표시되면 바로 내보기 컨트롤러가 해제됩니다.

또한 shouldAutorotateToInterfaceOrientation과 같은 메시지가 표시되지 않습니다.

내의 ViewController의 .H :

#import <UIKit/UIKit.h> 

@interface IssueViewController : UIViewController 
{ 
    IBOutlet UIBarButtonItem *button; 
} 

@property (nonatomic, readonly) UIBarButtonItem *button; 

- (IBAction)buttonTapped:(id)sender; 

+ (void)showSelfInView:(UIView *)view; 

@end 

그하는 .m :

#import "IssueViewController.h" 

@interface IssueViewController() 

@end 

@implementation IssueViewController 

@synthesize button; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.button.target = self; 
    self.button.action = @selector(buttonTapped:); 

} 

- (void)viewDidUnload 
{ 
    NSLog(@"unloaded"); 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)dealloc 
{ 
    NSLog(@"Got dealloc"); 
} 

+ (void)showSelfInView:(UIView *)view 
{ 
    IssueViewController *ivc = [[IssueViewController alloc] init]; 
    [view addSubview:ivc.view]; 
} 

- (IBAction)buttonTapped:(id)sender 
{ 
    [self.view removeFromSuperview]; 
} 


@end 

코드 2보기의 표시를 트리거하는 데 사용은 :

[IssueViewController showSelfInView:self.view]; 

누구나 난 알고 잘못하고있는거야? 보기가 제거 될 때까지 내 UIViewController이 유지되지 않는 이유는 무엇입니까?

편집 내가 ARC에 대해 알고

강한 & 약한 참조 ... 비 ARC-코드에서 showSelfInView에서 : 내가보기 컨트롤러를 유지하는 것, 내가 buttonTapped 그것을 autorelease를합니다.

저에게 그것을 달성하는 좋은 방법입니다. 그리고 내가 ARC, 또는 내 view/viewController 사용하는 방식으로 뭔가를 놓치고 있는지 알고 싶습니다. 뷰가 여전히 보이기 때문에, 뷰 콘트롤러는 dealloc-ed를 가져서는 안된다. 보기 컨트롤러에 대한 내 자신의 강력한 참조를 만드는 것 외에이를 방지 할 수있는 방법이 있습니까?

의보기가 디스플레이에서 제거됩니다 때까지 할당 된 상태로 유지하는 뷰 컨트롤러가 어떤 비 누덕 누덕 기운 비 더러운 방법이 있나요을 고쳐. 난보기 컨트롤러에서 자체로 포인터를 고려하는 더러운, 비록 내가 현재 사용하고있는 방법입니다.

답변

0

호가 인스턴스를 정의한 범위의 끝에서 즉시 해제하지 않게하려면 반드시 IssueViewController의 인스턴스에 대한 강력한 참조를 유지해야합니다.

+0

그렇습니다. 지금은보기 흉한 패치가 있습니다 : viewPadLoad에서 초기화되고'buttonTapped'에서 nil로 설정 한'@property (nonatomic, strong) IssueViewController * mySelf'입니다. 이것은 잘 작동하지만, 나는 그것이 그것을 이루기 위해 그렇게 추한 방법이 아니어야한다고 생각하기 때문에이 질문을하기로 결정했다. :) – user1532080

0

답변 : 다른 viewcontroller보기의 하위보기로 viewcontroller의보기를 추가하는 것은 좋지 않으므로 피해야합니다.

관련 문제