2009-07-26 7 views
0

私はそれぞれの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; 
} 

答えて

0

さて、最初のオフ、文字のUIを使用して、クラスの接頭辞はありません。 UIKit用に予約されています。 Objective-Cには名前空間がないため、よく知られている接頭辞(特にAppleが使用する接頭辞)を使用するのは悪い考えです。

ここにはいくつかのオプションがあります。

すべてのクラスがそれから派生し、作成時に(UIMainViewController *のような)いくつかの共有コントローラオブジェクトを取り、そのクラスが別の方法で共有オブジェクトに照会するようにUIViewControllerサブクラスを作成できます。それは依存関係の性質を逆転させます。

@interface CoreViewController : UIViewController { 
    MainViewController *mainViewController; 
} 

@property (nonatomic, retain, readonly) MainViewController *mainViewController; 

@end 

@implementation CoreViewController 

@synthesize mainViewController; 

- (id) initWithMainViewController:(MainViewController *)mainVC { 
    self = [super initWithNibName:nil bundle:nil]; 

    if (self) { 
    mainViewController = [mainVC retain]; 
    } 

    return self; 
} 

- (void) dealloc { 
    [mainViewController retain]; 

    [super dealloc]; 
} 

@end 

そして、self.mainViewControllerからデータを取得してください。あなたのアプリの性質によっては、カップリングが非常に強くなる可能性があります。別のオプションは、すべての種類の共有状態を、すべてが話すシングルトンに押し込むことです。これは、あなたのオブジェクトのすべてを明示的にチェックするのではなく、変更のためにシングルトンウォッチにKVOオブザーバーを登録させることができるという利点があります。

+0

UI名前空間を使用することを指摘してくれてありがとう。私のコードでは使用しないでください。ちょうどクイックネームを使用していました。理想的には、お互いを知りたくないので、彼らは自由に差し込めて遊ぶことができます。このユースケースに依存関係があるというだけのことですが、そのためにカップリングを開発する必要はありません。つまり、CoreViewControllerを知っているMainViewControllerを廃止できれば、それをやりたいのです。 – Boon

+0

それを考えると、おそらく私がするべきことは、MainViewControllerがCoreViewControllerのデリゲートを実装するために宣言し、CoreViewControllerがデリゲートを介してMainViewControllerを介してデータを取得するという規則的なデリゲートパターンです。 – Boon

関連する問題