2014-07-23 8 views
0

내 질문은 최대 후속 someone else's question입니다. @ritch 자신의 질문을 제공하는 이미지에서 그는 다음 뷰 컨트롤러가컨테이너 뷰를 제어하기 위해 SegmentedControlValueChange를 구현하는 방법

내 질문에 대한
"View Controller" -> (Container View)"View Controller" ->["First Controller", "Second Controller"] 

, 나는 그래서 방법을 구현하기 위해 노력하고

"Parent Controller" -> (Container View)"Child Controller" ->["First Controller", "Second Controller"] 

로 다시 작성합니다

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender 
{ 
} 

논리적으로이 메서드는 "부모 컨트롤러"에 있어야한다고 생각했지만 "자식 컨트롤러"에는 displayContentController

이 있어야합니다.
FirstController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"]; 

누군가가 나를 위해 명확히 주시겠습니까 : SegmentedControlValueChange 및 instantiateViewControllerWithIdentifier 사이에 "부모 컨트롤러"의 시간과 m 파일에 간다 무엇 코드

  • 를?
  • "부모 컨트롤러"의 h 및 m 파일에는 어떤 코드가 들어 있습니까?

답변

0

마지막으로 나는 스토리 보드 식별자를 사용하는 느낌이 약간 '추한'것이고 부정확하다는 생각이 들었으므로 내 질문에서 언급 한 것과 다른 접근 방식을 취했습니다. 그래서 여기

내가 무슨 짓을 :

내가 컨테이너보기에 표시 할 거라고 뷰 컨트롤러 XIB 파일과 클래스를 생성하여 시작했다. (예 FirstController, SecondController 등)

그때 제의 ViewController의 viewDidLoad에 방법에 넣고

(상위 뷰 컨트롤러 - 분할 제어 한) I는 다음 세 가지 방법을 설정

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // First Controller 
    self.firstViewController = [[FirstViewController alloc] init]; 

    // Second Controller 
    self.secondViewController = [[SecondViewController alloc] init]; 

    // Add the controllers to an Array 
    self.controllers = @[self.firstViewController, self.secondViewController]; 

    // Set the container to show the first view controller on load 
    [self displayContentController:[self.controllers firstObject]]; 
} 

컨테이너보기에 대한보기의 표시 및 숨기기를 처리하기 위해

- (void)displayContentController:(UIViewController *)content 
{ 
    [self addChildViewController:content]; 
    content.view.frame = [self frameForContentController]; 
    [self.view addSubview:content.view]; 
    [content didMoveToParentViewController:self]; 

    // Set current controller 
    self.currentController = content; 
} 

- (void)hideContentController: (UIViewController*)content 
{ 
    [content willMoveToParentViewController:nil]; 
    [content.view removeFromSuperview]; 
    [content removeFromParentViewController]; 
} 

- (CGRect)frameForContentController 
{ 
    return self.contentController.frame; 
} 

마지막으로, 다른 세그먼트 제어 값이 선택되면 이벤트를 처리했습니다.

- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender 
{ 
    // Hide current view controller 
    [self hideContentController:self.currentController]; 
    // Show new selected view controller 
    [self displayContentController:[self.controllers objectAtIndex:sender.selectedSegmentIndex]]; 
} 

희망이 있습니다.

+0

"부모 컨트롤러"-> (컨테이너보기) "자식 컨트롤러"-> [ "첫 번째 컨트롤러", "두 번째 컨트롤러"] 새 프로그램에 "상위 컨트롤러"-> [ "첫 번째 컨트롤러", "두 번째 컨트롤러"]'? 그렇다면 첫 번째 또는 두 번째가 화면에 표시되어야하는 위치를 어떻게 알 수 있습니까? 또는 버튼이 탐색 바에 있기 때문에 문제가되지 않습니다. 이는 내가 가지고있는 것과 다릅니다. 내 탐색 막대가 내 단추를 잡고 있지 않습니다. 부모보기 컨트롤러에 몸 단추가 있습니다. –

+0

@KatedralPillon '상위 컨트롤러'-> (컨테이너보기) -> [ "첫 번째 컨트롤러", "두 번째 컨트롤러"] '와 같습니다. 본문에 세분화 된 컨트롤러가있는 경우 전체보기를 차지하지 않도록 컨테이너보기의 크기를 조정할 수 있습니다. – ritch