質問

FirstController.m

- (IBAction)done:(id)sender {

    NSNotification *msg = [NSNotification notificationWithName:@"addNevItem" object:[NSString stringWithFormat:@"%i",1]];
[[NSNotificationCenter defaultCenter] postNotification:msg];

}

TwoController.m

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(addNevItem:)
                                             name:@"addNevItem"
                                           object:nil];
}

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

    NSLog(@"dd");

}

If the action is performed once, in the console I see one message. If the action is performed two times, in the console I see two more. If the action is performed three times, in the console I see three more. Why is this happening? I use the same code in other parts of the program and there is always only one message.

役に立ちましたか?

解決

you post a notification every time the action is executed so naturally you get just as much notifications

BUT

You have forgotten (or not shown ;)) to call removeObserver so notifications might 'pile up' (every living VC gets the notification)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top