2011-01-26 6 views
0

モデル(brain.h/m)にいくつかの情報を実行して取得させるためにRootViewController(ナビゲーションベースのアプリケーション)があります。明らかに、私は最初にモデル変数をインスタンス化しました。Objective-C:1つのモデルと様々なビューコントローラーを使用するMVC

これはRootViewController.hインタフェースである:

#import <UIKit/UIKit.h> 
#import "Brain.h" 


@interface RootViewController : UITableViewController 
{ 
Brain *cerebro; 
} 

@property (nonatomic, retain) Brain *cerebro; 

@end 

Iは、ユーザが最初viewcontrollertableviewの行をタップしたときに表示される詳細ビューを制御するために、第二viewcontrollerを追加しました:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

/* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 

最初のviewcontrollerが2番目のviewcontrollerで再びインスタンス化せずにインスタンス化したことをモデルに照会して尋ねることはできますか?

答えて

1

connectivity rules of capabilities programmingから1ページを取得し、ルートコントローラにBrainを導入して、下位コントローラに与えることができます。

紹介:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    <#DetailViewController#> *detailViewController 
     = [[<#DetailViewController#> alloc] 
       initWithNibName:@"<#Nib name#>" 
         bundle:nil]; 
    // introduce the Brain 
    detailViewController.brain = ref.to.rootController.brain; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 

養老:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    <#DetailViewController#> *detailViewController 
     = [[<#DetailViewController#> alloc] 
       initWithNibName:@"<#Nib name#>" 
         bundle:nil 
         brain:ref.to.rootController.brain]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
+0

ありがとうたくさんの男。私はあなたのヒントで問題を解決しました。 – Samui

+0

@outis - 'ref.to'構文は何ですか? –

0

私は私はあなたの質問を理解してわからない....しかし、私はあなただけのモデルクラス(ES)のために、あなたの第二のコントローラでプロパティを定義し、例えば、それを設定しないのはなぜ:)

を試してみてくださいあなたの最初のコントローラのviewDidLoad中に。

または、すべてのコントローラでデータモデルクラスへの参照があります。データモデル管理のための良い場所は、AppDelegateです。

Xcodeの「分割ビューベースのアプリケーション」テンプレートに基づいてアプリケーションを作成すると、良い例が得られます。

0

あなたはグローバルVARのようにそれを作るためにAppDelegateに直接参照を追加してみてくださいすることができます。

すなわち:

[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] cerebro]

SH:どこかのコードで

Brain *cerebro;

@property (nonatomic, retain) Brain *cerebro;

そして:AppDelegateインタフェースで

ouldはうまくいく...


あなたは何回も使用している場合、あなたは多くのBrainが、あなたがこのように使用することができBrainManagerシングルトンを実装するためには良い考えかもしれません使用している場合のみ1 Brainシングルトン

の実装について考えます:

Brain *cerebro = [[Brain alloc] init...] 
[[BrainManager sharedManager] addBrain:cerebro withIdentifier:@"cerebro"]; 

そして、どこか別の場所:

[SomeThing DoTaskWithBrain:[[BrainManager sharedManager] brainWithIdentifier:@"cerebro"]]; 
関連する問題