簡體   English   中英

未從模態ViewController接收到NSNotification

[英]NSNotification Not Received from Modal ViewController

我在這里想念什么? 我只是想將一個簡單的通知從模式視圖控制器發送回啟動它的視圖控制器,但是什么也沒收到。

這是視圖控制器中啟動模態搜索的代碼:

- (IBAction) chooseSuperGroup:(UIButton *)sender {
    NSLog(@"super group choice about to be made");

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(choiceReceived:)
                                                 name:@"selectionMade"
                                               object:self];
}

- (void) choiceReceived: (NSNotification *) notification
{
    NSLog(@"here");
    if ([[notification name] isEqualToString:@"selectionMade"]) {
         NSLog(@"received");
         NSLog(@"%@", (NSString *)[notification userInfo]);
    }

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: @"selectionMade"
                                                  object:self];
}

然后,在模式視圖控制器中,當用戶從表視圖中選擇一個單元格時,將執行以下代碼:

NSDictionary *dict = [NSDictionary dictionaryWithObject:selection forKey:@"superGroup"];

NSLog(@"printing dictionary contents");
for (id key in dict) {
    NSLog(@"key: %@   object: %@", key, [dict objectForKey:key]);
}

[[NSNotificationCenter defaultCenter] postNotificationName:@"selectionMade" object:self userInfo:dict];

我的輸出如下所示:

Super group choice about to be made
printing dictionary contents
key: superGroup   object: myChoice

因此,選擇被捕獲並添加到字典中。 但是沒有證據表明收到了任何通知。 這可以不那么難,但是我沒看到我的錯誤。 有人能幫我嗎? 謝謝!

嘗試使用“無”而不是“自我”

//添加觀察者

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

//刪除觀察者

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: @"selectionMade"
                                                  object:nil];

//發布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"selectionMade" object:nil userInfo:dict];

參考: https : //stackoverflow.com/a/8188759/2449268

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM