0

My NSNotification is delayed.

In myVC, I have two buttons, buttonA and buttonB. Each one is linked to their respective pdfs, pdfA and pdfB. There is a button Push, which is pressed after A or B is pressed. Push brings the user to the RVC where there is a UIWebView.

I want it so that by pushing either A or B, the UIWebView will display the respective pdf file. To debug this, I set it so that instead of changing the pdf, it will display text using NSLog. However, it doesn't work until after go back to myVC from the RVC by pressing a different button.

in myVC.m file:

- (IBAction)open_pictures_A:(id)sender
{

    //do some button alert popup action/whatever button does


    RootViewController *dataObject = [RootViewController new];

    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:dataObject
                                                         forKey:@"buttonA"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                                        object:nil
                                                      userInfo:userInfo];



}

And there is one like that for buttonB, but forKey would be "buttonB"

in the viewDidLoad for myVC.m I have

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                                        object:nil];

in my RVC,

RVC.h I have

@property (nonatomic, strong) NSString *property1;

And in the RVC.m I have

- (void)viewDidLoad
{

    // other stuff

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(eventListenerDidReceiveNotification:)
                                                 name:@"MyNotification"

                                               object:nil];
 }




- (void)eventListenerDidReceiveNotification:(NSNotification *)notif
{
    if ([[notif name] isEqualToString:@"MyNotification"])
    {
        NSLog(@"Successfully received the notification from buttonB!");

        NSDictionary *userInfo = notif.userInfo;
        RootViewController *dataObject = [userInfo objectForKey:@"buttonB"];

        // Your response to the notification should be placed here
        NSLog(@"dataObject.property1 -> %@", dataObject.property1);

    }
}

However, the log entry only shows up when I press a button to exit out of the RVC back to myVC

Here is the code I use to go from mVC to RVC

-(IBAction)goToRVC{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:@"Root"];
    [UIApplication sharedApplication].delegate.window.RootViewController = RVC;

    }

Then from RVC to myVC

-(IBAction)backtomyVC{


     [[[UIApplication sharedApplication] delegate] performSelector:@selector(myVC)];
    [self disconnect];

}

Why is my notification action being delayed?

user3325170
  • 133
  • 10
  • 1
    My question for you is why are you going through so much effort to get back to your rootViewController? If you are using the viewController stack properly then you code accomplish all of this by adding a push or modal segue from your first viewController to your webView's viewController. – Aron C Jun 30 '14 at 14:11

1 Answers1

0

It's not because of delay. When you press your button A or B you create new RVC object and you post the notification however you don't present/push RVC view controller (you just initialise it) so the RVC hasn't fired viewDidLoad method and it hasn't register itself as an observer.

After that you call goToRVC method where you create RVC object and you add it to the view hierarchy so the viewDidLoad method is call and the object register itself as an observer.

When you go back to mVC the RVC is not deallocated yet so it receive the notification and you can see the log.

Hope it's clear.

Greg
  • 25,317
  • 6
  • 53
  • 62