我使用如下代码订阅我自己的通知:
NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate {
Console.WriteLine("BaseFolderViewController: idle timer fired");
});
要发送通知,请执行以下操作:
NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);
但是,只有当PostNotificationName(string sString, object anObject)
的"anObject“参数不为空时,才能正确接收通知。
这是设计出来的吗?我必须传递一个对象吗?还是说这是个bug?我并不是真的想发送一个对特定对象的引用。
发布于 2011-10-01 00:20:29
我认为这是设计好的。苹果关于other overload (postNotificationName:object:userInfo:)的文档指出,userInfo参数可以为null。所以我认为其他两个不能为空。
"anObject“参数是发布通知的对象(发送者),也是可以从NSNotification类的object参数中检索的对象。
发布于 2012-05-21 23:58:01
这是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)
{
}
https://stackoverflow.com/questions/7612166
复制相似问题