2011-09-30 37 views
2

我用這樣的代碼訂閱我的通知:帶NULL對象的NSNotificationCenter.PostNotificationName()不會觸發:錯誤還是設計?

NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate { 
    Console.WriteLine("BaseFolderViewController: idle timer fired"); 
}); 

發送通知:

NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null); 

然而,該通知將只正確如果「anObject」參數的接收PostNotificationName(string sString, object anObject)不是NULL。

這是設計嗎?我必須傳遞一個對象嗎?或者它是一個錯誤? 我真的不想發送對特定對象的引用。

回答

0

我認爲這是設計。 Apple的其他過載文檔(postNotificationName:object:userInfo:)指出userInfo參數可以爲空。所以我想其他兩個不能爲空。

「anObject」參數是發佈通知(發件人)的對象,以及可以從NSNotification類的Object參數中檢索的對象。

+0

聽起來很合理。 – Krumelur

5

這是MonoTouch中的一個錯誤。 NSNotification是建立的,所以你可以發送一個可選的字典和一個可選的對象,通常是發送者,但也可以是其他對象。這兩個都可以爲null,但在MonoTouch中傳遞null作爲對象參數導致空指針異常。

從iOS文檔中可以很清楚地看到Object參數: 與通知關聯的對象。這通常是發佈此通知的對象。它可能是零。

public void SendNotification() 
{ 
    NSNotification notification = NSNotification.FromName("AwesomeNotification",new NSObject());    
    NSNotificationCenter.DefaultCenter.PostNotification(notification); 
} 

public void StartListeningForNotification() 
{ 
    NSString name = new NSString("AwesomeNotification"); 
    NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("AwesomeNotificationReceived:"),name,null);    
} 

public void StopListeningForNotification() 
{ 
    NSNotificationCenter.DefaultCenter.RemoveObserver(this,"AwesomeNotification",null);    
} 

[Export("AwesomeNotificationReceived:")] 
public void AwesomeNotificationReceived(NSNotification n) 
{ 

} 
相關問題