2011-04-05 4 views
0

나는이 서브 뷰목적 C : 열린 두 번째 시간 문제 하위 뷰를

-(IBAction) closeListClient { 
[self.view removeFromSuperview]; 

}

이 괜찮은지를 닫 하위 뷰를

- (IBAction) showList:(id) sender { 

if(list == nil){ 

    list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil]; 
    [list setDelegate:self]; 
    [self.view addSubview:list.view]; 
} 
} 

를 열려면이 코드이 코드를 처음으로,하지만 내가 subview를 열려고 할 때 두 번째로, 그것은 작동하지 않습니다, 왜?

답변

0

listnil이 아니기 때문에 if (list == nil) 안에 들어 가지 않습니다.

if (list.superview == nil)으로 변경하십시오.

+0

이 작업은 가능하지만'showList'가 호출 될 때마다 ListClient 객체가 누설됩니다. – mvds

0
-(IBAction) closeListClient{ 
[self.view removeFromSuperview]; 
if (list != nil) { 
    [list release]; 
    list=nil; 
}} 
+0

이 작업은 가능하지만 해산 될 때마다 ListClient 객체가 누출됩니다. – mvds

0

이것은 if 문 때문입니다. 변경 :

if (list == nil) { 
    list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil]; 
    [list setDelegate:self]; 
} 
[self.view addSubview:list.view]; 

그리고 작동합니다.

추신. 당신이

self.list = [[[ListClient alloc] initWithNibName:@"ListClient" bundle:nil] autorelease]; 

을 말할 수 있도록이의 메모리 관리 문제를 해결하려면, 클래스에 보관 된 재산 list을 추가하고 당신이 그것을 완료하면 당신은 객체를 해제 self.list = nil;을 말할 수있다. (예 : 해지하거나 dealloc 방법에서)

관련 문제