2013-10-17 69 views
1

我在我的應用程序中有一個UITableview控制器,一個視圖控制器,我試圖從UITableview控制器通過NSDictionary到我的ViewController,使用NSNotificationCenter。所以,我在我的UITableView控制器推送通知,然後我添加一個觀察者,用我的ViewController.The選擇的選擇器稱爲,但我有一個的NSLog並獲得記憶的結果,如:NSNotificationCenter,打印內存地址

的ViewController:0x8a0bcc0

我試圖通過NSString而不是NSDictionary,但我得到了內存結果,而不是字符串的值。

我的代碼:

的UITableView控制器

NSString *[email protected]"This is a test string"; 
    [[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string]; 

的ViewController

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self]; 

這裏是incomingNotification選擇方法:

-(void) incomingNotification : (NSNotification *)notification{ 
    NSLog(@"Trying to print : %@",[notification object]); 
} 

所有通知發生在ViewDidLoad method.Thank you!

UPDATE

最後,我放棄了使用NSNotificationCenter和使用性能來傳遞數據,從我TableViewController改變了一下inheretence。不知道爲什麼通知沒有工作,因爲他們應該這樣做。謝謝大家,非常感謝您的建議和想法:)

+1

爲什麼在添加觀察者後立即在'ViewController'中發佈第二個通知?這就是爲什麼你在控制檯中得到這個字符串。那麼爲什麼沒有測試字符串 - 確保在添加觀察者之前在表格控制器中發佈通知(設置斷點以觀察它們的順序) – medvedNick

+0

您可以在ViewController中使用object self調用postNotification,以便打印出您自己的目的。 –

+0

如果我刪除ViewController中的第二個通知,那麼選擇器方法不會被調用!我將嘗試設置斷點! –

回答

1
[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self] 

對象表示生成通知的對象。要發佈的參數使用另一種方法

[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self userInfo:string] 
+0

謝謝你的建議,但再次選擇器方法不被調用,當我刪除多餘的ViewController觀察員。當我有它 - 現在你的建議 - ,我再次得到內存地址結果。 –

+0

'userInfo'必須是一個字典,但這是一個小改變。 – Tommy

+0

是的,我不認爲這是問題:) –

0

如果我理解正確的話,UIViewController顯示你點擊按鈕後UITableViewController。而且,如果您在-viewDidLoad:中添加ViewController作爲觀察者,則只有在加載時才能收到通知。

你需要什麼:

1)覆蓋-initViewController-initWithNibName:方法是這樣的:

-(id) init 
{ 
    self = [super init]; 
    if (self) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil]; 
    } 
    return self; 
} 

所以你可以肯定ViewController是從一開始就觀察的通知(當然,這可能你的情況是不必要的步驟)

2)當你推ViewController你需要發送一個通知後,它是這樣的:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ViewController *nextController = [[ViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:nextController animated:YES]; 

    NSString *[email protected]"This is a test string"; 
    [[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string]; 
} 

但是,如果你只是想從一個視圖控制器發送一些參數到另一個,這是錯誤的方式。只需創建一個屬性ViewController和方法-tableView:didSelectRowAtIndex:UITableViewController設置此屬性

+0

非常感謝您的建議!您已經完全理解了這種情況。我嘗試了您所說的,但沒有發生任何事情。我的ViewController是從storyboard創建的,而我使用performSegueWithIdentifier從我的 - (void)tableView執行segue:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath委託方法。所以,我不應該分配ViewController.Moreover,我使用NSNotificationCenter的原因是,我無法將ViewController繼承到UITableviewController並從中獲取信息! –

+0

尼克,我也嘗試了initwithCoder方法。這也不起作用。 –

+0

嘗試在'ViewController'的'-prepareForSegue:sender:'方法中訂閱通知,並在'-performSegueWithIdentifier'之後立即發佈通知。你也可以從segue獲得'ViewController':'[segue destinationViewController];'所以你可以設置屬性而不是使用通知 – medvedNick