سؤال

This is my first attempt with NSNotification, tried several tutorials but somehow it's not working.

Basically I am sending a dictionary to class B which is popup subview (UIViewController) and testing to whether is has been received.

Could anyone please tell me what am I doing wrong?

Class A

- (IBAction)selectRoutine:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"Right"
                                                           forKey:@"Orientation"];
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"PassData"
     object:nil
     userInfo:dictionary];

    createExercisePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createExercisePopupView"];

    //Tell the operating system the CreateRoutine view controller
    //is becoming a child:
    [self addChildViewController:popupController];

    //add the target frame to self's view:
    [self.view addSubview:popupController.view];

    //Tell the operating system the view controller has moved:
    [popupController didMoveToParentViewController:self];

}

Class B

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(receiveData:)
     name:@"PassData"
     object:nil];
}

- (void)receiveData:(NSNotification *)notification {
    NSLog(@"Data received: %@", [[notification userInfo] valueForKey:@"Orientation"]);
}
هل كانت مفيدة؟

المحلول

If it hasn't registered to receive that notification yet - it will never receive it. Notifications don't persist. If there isn't a registered listener, the posted notification will be lost.

نصائح أخرى

Specific to your problem, the receiver hasn't started observing before the notification is sent so the notification just gets lost.

More generally: What you're doing wrong is using notifications for this use case. It's fine if you're just playing around and experimenting but the kind of relationship you're modelling here is best actioned by retaining a reference to the view and calling methods on it directly. It's usually best if experimentation is realistic of the situation in which it would actually be used.

You should be aware of 3 basic communication mechanisms and when to use them:

Notifications Use them to notify other unknown objects that something has happened. Use them when you don't know who wants to respond to the event. Use them when multiple different objects want to respond to the event.

Usually the observer is registered for most of their lifetime. It's important to ensure the observer removes itself from NSNotificationCenter before it is destroyed.

Delegation Use delegation when one object wants to get data from an unknown source or pass responsibility for some decision to an unknown 'advisor'.

Methods Use direct calls when you know who the destination object is, what they need and when they need it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top