带有NULL对象的NSNotificationCenter.PostNotificationName()不会触发:bug或设计?

Kru*_*lur 2 c# xamarin.ios nsnotificationcenter

我正在使用以下代码订阅我自己的通知:

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

要发送通知:

NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);
Run Code Online (Sandbox Code Playgroud)

但是,只有在"anObject"参数PostNotificationName(string sString, object anObject)不为NULL 时才会正确接收通知.

这是设计的吗?我必须传递一个物体吗?或者这是一个错误?我真的不想发送对特定对象的引用.

Ske*_*ela 5

这是MonoTouch中的一个错误.构建NSNotification以便您可以发送可选字典和可选对象(通常是发送方),但也可以是其他对象.这两个都可以为null,但在MonoTouch中传递null,因为object参数会导致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)
{

}
Run Code Online (Sandbox Code Playgroud)