2011-06-09 41 views
0

所以,我只是在各種情況下測試NSNotifications,這是令人困惑的。如果你能幫助我理解NSNotifications,我將不勝感激!視圖控制器有時不會收到一個NSNotification

我有一個導航控制器。

我有一個名爲「添加」的UIBarButtonItem,

如果我點擊添加它推我view2的哪些帖子通知DidAddNotification。

// 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// 

從View 3,我popToRootViewControllerAnimated:是,我回到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. 

我再次按Add:

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 
+1

請張貼代碼,您的問題太長,難以理解。 – 2011-06-09 19:04:33

+0

請告訴我哪一部分很難理解,我會盡我所能編輯併發回。 – Legolas 2011-06-09 19:09:53

+0

奇怪的是,只有某些通知會觸發。你如何設置觀察員? – justin 2011-06-09 19:32:01

回答

1

」第一次發送通知時,其他視圖控制器不存在,它們還沒有被創建,viewController仍然是零,因爲沒有觀察者對象,所以你沒有得到任何第二次,視圖控制器中的兩個對象都已創建,所以他們在收到通知時會收到通知,因爲它們處於活動狀態,並記錄接收到的通知語句。

3

你所描述的似乎是差異歸因於哪些對象是活着的時候的變化。視圖和視圖控制器不會無限期地存在,並不是在應用程序啓動時全部創建。一個對象必須存在才能接收和記錄通知。基本通知系統按預期工作。

如果您添加日誌語句以宣告何時應創建一個應接收這些通知中的一個的對象,並且該對象在-init(或)主體內被銷燬時,您應該能夠看到生命週期對收到的消息的影響無論你的超類的指定初始值是什麼)和-dealloc

另外:如果使用日誌記錄功能(例如NSLog(@"%s: <message>", __func__))標記日誌語句,您的日誌語句將更容易追蹤。編譯器爲每個包含該函數名稱的函數生成一個名爲__func__的字符串。

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次(分別爲一,二和三,),是幾乎完全一樣的。標題中沒有任何內容(除標準內容外)。我將發佈其中一個.m文件,以便您可以看到設置。

#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]; 
} 

而且說實話,這幾乎是它。在我的第三課,我彈出到根控制器,而不是創建一個新的視圖控制器,但就是這樣。它會告訴你每次點擊該按鈕後你會看到哪個視圖,所以希望它能幫助你更好地理解通知的工作方式。 「

+0

非常感謝時間花花公子。我不知道爲什麼這發生在我身上。我知道這不可能是原因 - (大聲笑),但讓我問你這個...你使用哪種Xcode?我正在使用ios5 sdk beta的最新版本。 – Legolas 2011-06-09 20:45:55

+0

哈哈,我不知道你的情況怎麼樣。我在iOS 4.3中使用Xcode 3,所以這可能是原因。我有點懷疑,但你永遠不知道。根據WWDC的說法,我知道他們改變了通知系統,但我認爲這與NSNotificationCenter沒有任何關係,但你永遠不知道 – justin 2011-06-09 20:53:33

+0

那麼。它們的可能性非常低,它們會在ios5 sdk中改變這種情況。大聲笑。如果我把這個放在developer.apple.com上,問他們他們會有可能發生LOL。我會再坐下來分析一下代碼....(嘆氣)!感謝幫助夥計。 – Legolas 2011-06-09 20:57:21

相關問題