각 UIViewController 파생 클래스에 연결하는 각 TabBarItem있는 UITabBarController 들어있는보기 컨트롤러 (하자 그것을 MainViewController 호출하자). 특정 UIViewController의 경우 MainViewController를 통해 그 당시 값을 전달하려고합니다. 이 프록시를 수행하는 가장 좋은 방법은 무엇입니까?부모 ViewController를 통해 ViewController에 값을 프록시하는 것이 가장 좋습니다.
지금은 MainViewController에서 프록시 용도로 속성을 만들어야합니다. 그러나 이상적으로, MainViewController는 값을 전달해야하는 UIViewController의 특정 유형과 전달되는 값의 특정 유형을 알지 못합니다. 값은 MainViewController에서 사용되지 않으므로 불필요한 것으로 간주됩니다.
탭 막대 항목 2가 UIEmployeeInfoViewController 클래스에 연결한다고 가정 해 보겠습니다. UIEmployeeInfoViewController는 EmployeeInfo라는 유형의 객체에 관심이 있습니다.
여기에 내 현재 솔루션이지만 나는 더 나은 방법을 찾아 일을 피하려고 노력하고있는 작업은 다음과 같습니다 첫째 좋아
1) Somewhere where UIMainViewController is being create... UIMainViewController *mainViewController = [[UIMainViewController alloc] initWith...]; // a property called employeeInfo is created in UIMainViewController class so it can be forwarded later mainViewController.employeeInfo = employeeInfoObj; ... 2) Code to make UIMainViewController pass the employeeInfo along to UIEmployeeInfoViewController when the tabbar item is tapped: - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { if ([viewController class] == [UIEmployeeInfoViewController class]) { // Need to create coupling to UIEmployeeInfoViewController class // and to EmployeeInfo class as well ((UIEmployeeInfoViewController *)viewController).employeeInfo = self.employeeInfo; } return YES; }
UI 네임 스페이스를 사용하는 것에 대해 지적 해 주셔서 감사합니다. 내 코드에서 사용하지 마십시오. 빠른 이름 만 사용했습니다. 이상적으로, 나는 그들이 서로에 대해 전혀 알지 못하게하여 자유롭게 연결하고 연주 할 수 있습니다. 이 유스 케이스에 의존성이 존재하지만, 그 때문에 어떤 커플 링도 개발되기를 원하지 않는다. 즉, CoreViewController를 아는 MainViewController를 제거 할 수 있고 그 반대 인 경우, 그 작업을 수행하려고합니다. – Boon
생각해 보면 어떨까요? MainViewController가 CoreViewController의 대리자를 구현한다고 선언하고 CoreViewController가 위임자 (MainViewController)를 통해 데이터를 가져 오는 규칙적인 Delegate 패턴을 사용하면됩니다. – Boon