0

そのため、さまざまなケースで NSNotifications をテストしているだけですが、これは混乱を招きます。NSNotifications を理解するのを手伝っていただければ幸いです。

ナビゲーションコントローラーがあります。

「追加」という UIBarButtonItem があり、通知を投稿します DidAddNotification

[追加] をクリックすると、view2 にプッシュされます。

 // I add view2 as observer and write method for this and NSlog if it gets implemented //

私は再び 3 を表示するように自分をプッシュします。

// I add view3 as another observer and use the same method as the previous view and I NSlog if it gets implemented//

ビュー 3 から popToRootViewControllerAnimated:YES を実行すると、1 に戻り、同じ手順を繰り返します。

コントロールはこんな感じで・・・

1 -> 2 -> 3 -> 1

if I press add again,

the control is again the same 1 -> 2-> 3-> 1

出力 (NSLogs) は次のとおりです。

初めて [追加] を押します。

2011-06-09 14:47:41.912 Tab[5124:207]  I am the notification in view2
2011-06-09 14:47:41.912 Tab[5124:207]  I pressed Add Button and I just sent a notification from view 1
  // No notification in view 3 ?? //  I am now back to view 1.

もう一度 [追加] を押します。

2011-06-09 14:47:51.950 Tab[5124:207] I am the notification in view3
2011-06-09 14:47:51.951 Tab[5124:207]  I pressed Add Button and I just sent a notification from view 1
 // No Notification in view 2 ??? // ... I am now back to view 1.

[追加] をもう一度押します。

2011-06-09 14:47:59.160 Tab[5124:207] I am the notification in view 3
2011-06-09 14:47:59.161 Tab[5124:207]  I pressed Add Button and I just sent a notification from view 1

 // No Notification in view 2 ??? //  ... I am now back to view 1.


And this goes on..

誰か教えてくれませんか

  1. NSLog は最初はビュー 3 で印刷されませんでしたが、それ以外は常に印刷されますか?
  2. NSLog がビュー 2 で初めて印刷し、二度と印刷しないのはなぜですか?

コード:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DidAddNotification" object:self];  // I put this in the - (IBAction) for addData

- (void)didPressAdd:(NSNotification *)notification { //NSLogs// }

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPressAdd:) name:@"DidAddNotification" object:nil]; // I put this in the viewDidLoad of view 1 and view 2
4

3 に答える 3

3

あなたが説明している違いは、どのオブジェクトがいつ生きているかの変化によるものと思われます。ビューとビュー コントローラーは無期限に存在するわけではなく、アプリの起動時にすべてが作成されるわけではありません。通知を受信して​​ログに記録するには、オブジェクトが存在する必要があります。基本的な通知システムは期待どおりに機能しています。

これらの通知のいずれかを受信するはずのオブジェクトが作成されたとき、および本体内で破棄されたときを通知するログ ステートメントを追加すると、受信したメッセージに対する有効期間の影響を確認できるはずです-init(または指定された初期化子は何でも)あなたのスーパークラスは)と-deallocです。

また、 のようにロギングを行う関数でタグ付けすると、ログ ステートメントを追跡しやすくなりますNSLog(@"%s: <message>", __func__)。コンパイラ__func__は、関数の名前を含む関数ごとに指定された文字列を生成します。

于 2011-06-09T20:22:19.557 に答える
1

ナビゲーションベースのアプリをセットアップしました。ルート コントローラー ヘッダーには、次のように記述されています。

#import <UIKit/UIKit.h>

extern NSString * const EPNotification;

@interface RootViewController : UITableViewController {
}
@end

私が実際に行ったのは、コード全体で使用する文字列を設定したことだけです。次に、ルート実装ファイルに、これがあります (およびすべての標準的なもの):

#import "RootViewController.h"
#import "One.h"

NSString *const EPNotification = @"Notification"; // this will be the global string name for the notification

@implementation RootViewController


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotNotification:) name:EPNotification
                                           object:nil];

    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain                                                          target:self action:@selector(sendNotification)];

    self.navigationItem.rightBarButtonItem = next;
    [next release];
}

- (void)sendNotification {
    NSDictionary *d = [NSDictionary dictionaryWithObject:@"view 1" forKey:@"sender"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self userInfo:d];
    One *o = [[One alloc] initWithNibName:@"One" bundle:nil];
    [self.navigationController pushViewController:o animated:YES];
    [o release];
}

- (void)gotNotification:(NSNotification *)note {
    NSLog(@"from %@", [[note userInfo] objectForKey:@"sender"]);
}

他に 3 つのビュー (それぞれ 1 つ、2 つ、3 つ) がありますが、ほとんどすべて同じです。ヘッダーには何もありません(標準のもの以外)。セットアップを確認できるように、.m ファイルの 1 つを投稿します。

#import "One.h"
#import "Two.h"

@implementation One

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain
                                                     target:self action:@selector(sendNotification)];

    self.navigationItem.rightBarButtonItem = next;
    [next release];
}

- (void)sendNotification {
    NSDictionary *d = [NSDictionary dictionaryWithObject:@"view 2" forKey:@"sender"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self userInfo:d];
    Two *t = [[Two alloc] initWithNibName:@"Two" bundle:nil];
    [self.navigationController pushViewController:t animated:YES];
    [t release];
}

そして正直なところ、それはほとんどそれです。クラス Three では、新しいビュー コントローラーを作成する代わりに、ルート コントローラーにポップしますが、それだけです。ボタンをクリックするたびに、どのビューが表示されているかがわかるので、通知がどのように機能するかをよりよく理解するのに役立つことを願っています.

于 2011-06-09T20:27:02.750 に答える
1

「最初に通知を送信したとき、他のView Controllerは存在しません。それらはまだ作成されていません。viewControllerはまだnilです。オブザーバーオブジェクトがないため、ログは取得されません。2回目。 View Controller 内の両方のオブジェクトが作成されているため、それらは生きているときに通知を受け取り、受け取った通知ステートメントをログに記録します。」

于 2011-06-14T19:02:01.973 に答える