NSNotification被多次提升

A f*_*pha 10 iphone nsnotifications nsnotificationcenter ipad

我正在开发一个电子书阅读器,我遇到了以下问题.我正在使用IBAction方法发布NSNotification,一旦按下按钮,就会调用一个动作方法.第一次工作绝对正常...... IBAction方法必须被称为evry time我点击按钮.完成此操作后,我需要转到我的主页,然后再返回并再次单击该按钮.这是我面临的问题.该方法从第二次被调用不止一次,而且这个数字只会不断增加..请帮我解决这个问题....

//IBAction Method
if (sender.tag == PenToolAction) {

    GLogInfo(@"Pen tool tapped");
    [self hideSelf];
    [[NSNotificationCenter defaultCenter] postNotificationName:PenToolActionInitialized object:nil];
}
Run Code Online (Sandbox Code Playgroud)

Jil*_*ouc 35

如果在多次调用的方法中添加通知观察器,您也会多次收到通知.

尝试要么放[[NSNotificationCenter defaultCenter] addObserver:...]init方法来代替,或致电[[NSNotificationCenter defaultCenter] removeObserver:self]权利之前.