Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone addObserver:名称_Iphone_Ios_Xcode_Nsnotificationcenter_Addobserver - Fatal编程技术网

Iphone addObserver:名称

Iphone addObserver:名称,iphone,ios,xcode,nsnotificationcenter,addobserver,Iphone,Ios,Xcode,Nsnotificationcenter,Addobserver,我使用的是这样的addObserver: [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notificationReceived:) name:nil object: nil]; 一切都很好,但我认为当我不再需要观察器时,移除观察器可能是一种很好的形式。。。我发现我需要用这句话: [[NSNotificationCenter defaultCenter] removeObserver:se

我使用的是这样的addObserver:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notificationReceived:) name:nil object: nil];
一切都很好,但我认为当我不再需要观察器时,移除观察器可能是一种很好的形式。。。我发现我需要用这句话:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"observerName" object:nil];
问题是,当我将addObserver行更改为包含名称,以便removeObserver知道要删除哪个观察者时,将不再调用通知。此行已运行,但在添加名称时被忽略:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notificationReceived:) name:@"observerName" object: nil];
我可以将名称设置回nil,它会再次工作。 有人知道我做错了什么吗


谢谢

我认为您可能会误解为参数的含义

名称:
告诉系统要通知哪些通知

self
是实际的观察者,因此当您
removeOberserver:self
时,您将停止接收任何通知

您应再次仔细阅读该文档,了解其含义:


例如,请参见

,我认为您可能误解了参数的含义

名称:
告诉系统要通知哪些通知

self
是实际的观察者,因此当您
removeOberserver:self
时,您将停止接收任何通知

您应再次仔细阅读该文档,了解其含义:


例如,请参见

您确定名称中没有输入错误吗?与其说两次,您可能会遇到输入错误,不如使用
const NSString*notificationName=@“observer name”
并在所有位置使用它。您确定名称中没有输入错误吗?您可能会遇到两次输入错误,而不是两次输入错误,因此请使用
const NSString*notificationName=@“observer name”
并在所有地方使用它。谢谢-我已经读到,我不应该在没有name:参数的情况下调用removeObserver:self。感谢您让我知道name:用于选择我们要侦听的系统通知,现在更好了,因为我只收到了我特别想要的通知,而不是每个通知。我还可以在removeObserver:name:object:函数中使用通知名称,它可以正常工作。因此,您需要一个实际的通知,而不是@“myNotificationName”,如“\u UIImagePickerControllerUserDidCaptureItem”在addObserver和RemoveObserverThank的name:参数中-我读到,如果没有name:参数,我不应该调用removeObserver:self。感谢您让我知道name:用于选择我们要侦听的系统通知,现在更好了,我只收到了我特别想要的通知,而不是所有的通知。我还可以在removeObserver:name:object:函数中使用通知名称,它可以正常工作。因此,在addObserver和removeObserver的name:参数中,您需要一个实际的通知,而不是@“myNotificationName”,如“\u UIImagePickerControllerUserDidCaptureItem”