2016-08-11 6 views
0

説明のUIViewControllerデータを与えない:NSObjectの中に含まれるのUIViewControllerリンク機能に含まNSObjectのは、以下に示す問題の

  1. 機能
  2. 場合のUIViewController
  3. に含まれる他の機能に
  4. NSObjectの戻り値UIViewControllerを参照する関数UILabelを更新しません

UILabelがUIViewControllerで見つかりました

NSObjectクラス:

-(void)getCategoryId:(NSString *)categoryid { 
    categoryMap *catMap = [[categoryMap alloc] init]; 
    [catMap getCategoryId:categoryid]; 
    catMap.nePOI = categoryid; 
} 

のUIViewController:

-(void)getCategoryId:(NSString *)categoryid { 
    self.label.text = categoryid; 
} 
+0

@TejaNandamuri問題の例を書きました。デフォルトでは、いくつかのガイドラインがあります。私は機能するようにローカルにアピールし、適切に動作します。 – Kejl

+0

値が変更されると、デリゲート(プロトコル)を使用してデータを渡すことができます。また、NSNotificationCenterはそれを実行するための良いオプションです。 –

答えて

0

あなたは新しいcategoryMapのビューのcontrolerオブジェクトを作成していて、そのラベルではなく、現在されているcategoryMapビューコントローラから1を更新しています表示されます。 "NSObject"インスタンスは、実際に画面に表示されるViewControllerを指し示すreflen-rence(プロパティ)を必要とします。

-(void)getCategoryId:(NSString *)categoryid { 
    // View controller is created 
    categoryMap *catMap = [[categoryMap alloc] init]; 

    // label updated 
    [catMap getCategoryId:categoryid]; 
    catMap.nePOI = categoryid; 

    // End of the method, View controller is destroyed 
    // See the problem here??? You are not updated the good viewController... 
} 

これはおすすめの方法ではありませんが、あなたのviewControllerは、何を表示するかをオブジェクトに尋ねる必要があります。しかし、あなたの場合、通知でこれを修正することができます。

[[NSNotificationCenter defaultCenter] postNotificationWithName:@"categorySelected" object:categoryId]; 

によってオブジェクトクラスで、あなたのビューのcontrolerでそれにviewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCategoryId:) name:@"categorySelected" object:nil]; 

を購読して、getCategoryIdを置き換えます:getCategoryIdの実装を交換し、あなたのビューコントローラの方法によって:

-(void)getCategoryId:(NSNotification *)notif { 
    self.label.text = (NSString *)(notif.object); 
} 

これはすべきです;;)

ViewControllerの原則の背後にあるアーキテクチャをよりよく理解するには、Apple MVCのドキュメントを参照してください。https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

関連する問題