Ios NSNotificationCenter正在多次呼叫

Ios NSNotificationCenter正在多次呼叫,ios,objective-c,iphone,nsnotificationcenter,nsnotification,Ios,Objective C,Iphone,Nsnotificationcenter,Nsnotification,我已经在我的应用程序中实现了NSNotificationCenter。当图像解码完成时,我发送通知。第一次图像解码将执行8次。因此,该通知假定发送8次。但它调用了64次(8*8) 这是我如何实现的代码--> //初始化 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [[NSNotificationCenter defaultCenter] addObserve

我已经在我的应用程序中实现了NSNotificationCenter。当图像解码完成时,我发送通知。第一次图像解码将执行8次。因此,该通知假定发送8次。但它调用了64次(8*8)

这是我如何实现的代码--> //初始化

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
 if (self) {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                      selector:@selector(getHRImage:)
                                                             name:@"DecodeComplete" object:nil];}   
//调用方法

 -(void)getHRImage:(NSNotification *) notification
{

if ([[notification name] isEqualToString:@"DecodeComplete"])
    NSLog (@"Successfully received the DecodeComplete notification! ");
}`
//解除分配

- (void) dealloc
{
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
    //[super dealloc];
}
- (void) dealloc
  {
    note=true;

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:nil];
//[super dealloc];
}
//通知后

[[NSNotificationCenter defaultCenter] postNotificationName:@"DecodeComplete" object:self];
有人可以告诉我哪里做错了

提前谢谢

//调用方法如下(调用8次)

-(void)dealloc
将不会在ARC环境中调用。Instread,您可以在添加之前删除观察者,如下所示:

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHRImage:) name:@"DecodeComplete" object:nil];     
  }
}
解决方案: 我重新检查了我的代码,initWithFrame:(CGRect)frame调用了8次,添加了8次NSNotification observer

所以我改变了我的代码,就像这样,-->>初始化

static bool note=YES;
- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
  if(note)
 [[NSNotificationCenter defaultCenter] addObserver:self

                                                  selector:@selector(getHRImage:)
                                                         name:@"DecodeComplete" object:nil]; note=NO;}   
--->>解除分配

- (void) dealloc
{
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
    //[super dealloc];
}
- (void) dealloc
  {
    note=true;

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:nil];
//[super dealloc];
}
现在,addObserver方法只调用了一次,所以我的问题得到了解决。
谢谢大家的宝贵指导。

您没有显示足够的上下文来提供答案,但您必须调用此函数64次,因此我怀疑您的循环结构有问题。我建议您发布更多代码,或者在名为的
postNotificationWithName
上放置一个断点,然后查看调用它的位置您是否检查了
dealloc
方法是否被调用?似乎您多次添加了观察,而之前添加的观察没有被删除,因此您得到了多次getHRImage方法检查项目1中的一些点。触发通知时,内存中只存在一个对象。2.检查在发出火灾通知之前调用initWithFrame的次数。3. @没有人已经提到,最后一个在您发出通知时检查您的代码。@没有人在我返回时调用dealloc方法。我重新检查了没有多个观察者。dealloc将在ARC中调用,因此dealloc中的任何自定义仍然是合法的。你一定犯了一些错误。这不起作用有两个原因:1<代码>-dealloc按照@user3349433,2所述执行。它将删除新的视图实例,而不是现有的视图实例。