Search code examples
objective-cuiviewcontrollernsnotificationcenternsnotificationviewwillappear

How to use object received from NSNotification in viewWillAppear:


Here is my method that my ViewControllerA implements as part of a NSNotification system:

- (NSInteger)updateTortoiseLevel:(NSNotification*)notification {
    _updateValue = 0;
    if ([notification.name isEqualToString:@"gameToTortoise"]) {
        NSNumber* update = [notification.userInfo objectForKey:@"gameToTortoise"];
        updateValue = [update integerValue];
    } else {
        // do other things
    }
    return _updateValue;
}

First off, I don't even know if I can return anything from this type of method, so let me know if I can't.

Here's what I'm after:

- (void)viewWillAppear:(BOOL)animated {
    if (_update == 1) {
        [self runCellUpdates];
    }
}

I'm using the notification system when I pop ViewControllerB to A. What happens first?...Does notification get sent before viewWillAppear:? If it doesn't, how can I use updateTortoiseLevel: inside viewWillAppear: if at all?

Do not mention "use delegate design pattern instead" because I already considered it but it will not work for my current design (at least 95% sure on that). Cheers


Solution

  • First off, I don't even know if I can return anything from this type of method, so let me know if I can't.

    Okay: You can't. Think about it. Who is calling you here? It's the runtime, delivering a notification. So there is no one for you to return a value to. The runtime is not going to wait to hear if you have a value to hand back in response, and even if you did, what do you imagine the runtime would do with it? The notion of returning a value in this situation is meaningless.

    how can I use updateTortoiseLevel: inside viewWillAppear: if at all?

    The question seems meaningless, but in any case my response would be: why would you want to? You are presumably getting the updateTortoiseLevel message with the notification and the NSNumber packed inside it, so why don't you do whatever it is you want to do in response right then and there inside updateTortoiseLevel?