IT박스

View Controller Containment는 iOS 5에서 어떻게 작동합니까?

itboxs 2020. 8. 8. 12:08
반응형

View Controller Containment는 iOS 5에서 어떻게 작동합니까?


WWDC 2011 세션 (102), 애플은 유사한 사용자 지정보기 컨트롤러 컨테이너를 만들 수있는 기능이다보기 컨트롤러 봉쇄, 소개 UITabBarController, UINavigationController등을.

나는 예제를 여러 번 보았다. 이 패턴과 관련된 많은 방법이 있지만 정확히 파악하기가 조금 어려웠습니다. 나는 여기에 내가 생각하는 일을 게시하고 커뮤니티가 내 의심을 확인하거나 반박 할 것인지를 볼 것입니다.

시나리오 1 : 부모 없음에서 새 부모 뷰 컨트롤러로 이동

[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];

처음 두 줄은 주어진 순서대로 나타나야합니까, 아니면 반대로 될 수 있습니까?

시나리오 2 : 상위 뷰 컨트롤러에서 상위 뷰 컨트롤러 없음으로 이동

[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];

전화도 필요 [vc didMoveToParentViewController:nil]합니까? 세션 (102)의 예는 이 작업을 수행하지 않은 이 시나리오에서,하지만 난 그가 누락되었거나 여부를 알 수 없습니다.

시나리오 3 : 상위 뷰 컨트롤러에서 다른 컨트롤러로 이동

이는 각 상위 뷰 컨트롤러의 논리가 캡슐화되기 때문에 다음과 같은 방식으로 발생할 수 있습니다.

// In the old parent
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];

// In the new parent
[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];

질문

내 주요 질문은 이것이다 : 이것이 일반적으로 뷰 컨트롤러 포함이 어떻게 작동해야 하는가? 위에 주어진 메커니즘이 맞습니까?

전화 willMoveToParentViewController하기 전에 전화 addChildViewController해야합니까? 이것은 나에게 논리적 인 순서처럼 보이지만 반드시 필요합니까?

통화 didMoveToParentViewController:nil후 전화 removeFromParentViewController해야합니까?


UIViewController문서는 때와하지 않을 때 호출에 꽤 명확하다 willMove/ didMove방법. 아웃 확인 "컨테이너 뷰 컨트롤러 구현" 문서를.

문서에 따르면을 재정의 addChildViewController하지 않으면 willMoveToParentViewController:메서드 를 호출 할 필요가 없습니다 . 그러나 didMoveToParentViewController:전환이 완료된 후 메서드 를 호출해야합니다 . "마찬가지로, willMoveToParentViewController:메서드를 호출하기 전에 메서드를 호출하는 것은 컨테이너 뷰 컨트롤러의 책임입니다 removeFromParentViewController. 메서드는 자식 뷰 컨트롤러 removeFromParentViewController메서드를 호출합니다 didMoveToParentViewController:."

또한 여기에서 작업 한 예제 여기 에 샘플 코드가 있습니다 .

행운을 빕니다


이 부분은 올바르지 않습니다.

[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];

문서에 따르면 :

When your custom container calls the addChildViewController: method, it automatically calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it.

So you don't need the [vc willMoveToParentViewController:self] call. It is done automatically when you call [self addChildViewController:vc]. Here's the code sample again:

[self addChildViewController:vc];
// [vc willMoveToParentViewController:self] called automatically
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];

For removing view controllers:

The removeFromParentViewController method automatically calls the didMoveToParentViewController: method of the child view controller after it removes the child.

Presumably this call is [oldVC didMoveToParentViewController:nil].

[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
// [vc didMoveToParentViewController:nil] called automatically

참고URL : https://stackoverflow.com/questions/8379759/how-does-view-controller-containment-work-in-ios-5

반응형