私はそれぞれのTabBarItemがそれぞれのUIViewController派生クラスに接続するUITabBarControllerを含むビューコントローラ(MainViewControllerと呼ぶことにしよう)を持っています。特定のUIViewControllerの場合、MainViewControllerを介してその時点で値を渡したいと思います。このproxy'ingを行うための最善の方法は何ですか?親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を介してデータを取得するという規則的なデリゲートパターンです。 – Boon