2011-07-26 2 views
0

iPhone 시작 예제를 살펴보면서 단추를 누를 때 두보기간에 전환하는 코드가 있습니다.objective-c의 수퍼 뷰 제거

if (self.yellowViewController.view.superview == nil) 
{ 
    if (self.yellowViewController == nil) 
    { 
     YellowViewController *yellowController = 
     [[YellowViewController alloc] initWithNibName:@"YellowView" 
bundle:nil]; 
     self.yellowViewController = yellowController; 
     [yellowController release]; 
    } 
    [blueViewController.view removeFromSuperview]; 
    [self.view insertSubview:yellowViewController.view atIndex:0]; 
} 
else 
{ 
    if (self.blueViewController == nil) 
    { 
     BlueViewController *blueController = 
     [[BlueViewController alloc] initWithNibName:@"BlueView" 
bundle:nil]; 
     self.blueViewController = blueController; 
     [blueController release]; 
    } 
    [yellowViewController.view removeFromSuperview]; 
    [self.view insertSubview:blueViewController.view atIndex:0]; 
} 

그것은 나에게 의미가 없지만, 내가 가지고있는 문제는 네 개의 뷰가있는 UISegmentControl이 작업을 수행 할 방법입니다 : 여기에 자신의 예제 코드에서 첫 번째 단편이다. selectedSegment를 확인하고 필요한 경우 해당보기를 만들 수 있다는 것을 알고 있습니다. 하지만 내 새보기를 하위보기로 추가하기 위해 수퍼 뷰에서이를 제거하기 위해 마지막보기가 무엇인지 어떻게 알 수 있습니까? 감사!

+0

올바른 들여 쓰기를 사용하면이 코드를 훨씬 쉽게 읽을 수 있습니다. –

답변

0

어떤보기가 할당되어 있는지 확인하고 nil을 확인한 다음 제거 할 수 있습니다.

if (yellowController) { 

[yellowController.view removeFromSuperView]; 
[yellowController release]; 

} 

네 가지보기를 통해 어떤보기가로드되어 있는지 확인한 다음보기를 제거 할 수 있습니다.

1

각보기를 segmentIndex.so에 코드 또는 IB 세트 태그 값을 생성하는 동안 해당 태그 값으로 나중에 얻을 수 있습니다. 이것은 까다 롭고 간단합니다.

+0

이 시나리오에서는 마지막보기가 선택된 것을 기억하는 ivar을 사용하여 추적해야합니까? 고마워. – Crystal

0

UIView의 경우, 맨 앞 서브 뷰는 [[myView subviews] lastObject]입니다.

+0

... 매우 위험합니다. 프레임 워크 나 자신의 코드는 나중에 언제든지 다른 뷰를 추가 할 수 있습니다. 코드에 시한 폭탄을 두지 마십시오. – Eiko

관련 문제