Jack Nutkins
Jack Nutkins

Reputation: 1555

Problems displaying view controller when local notification received on iPhone

The following code is my latest attempt to display a view controller when my application receives a particular local notification:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"Notification Received.  UserInfo: %@", [notification.userInfo valueForKey:@"notificationType"]);
    if ([[notification.userInfo valueForKey:@"notificationType"] isEqualToString:@"backup"])
    {
        NSLog(@"Backup notification received.");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"Yes" forKey:@"shouldBackup"];
        [defaults synchronize];
        SettingsViewController *vc = [self.window.rootViewController.tabBarController.viewControllers objectAtIndex:3];
        [self.window.rootViewController.tabBarController.navigationController pushViewController:vc animated:NO];
    }
    else
    {
        NSLog(@"Did receive notification: %@, set for date:%@ .", notification.alertBody, notification.fireDate);
    }
}

I then have this piece of code which I use in the viewDidAppear method to determine wether or not to perform the backup:

if ([[defaults objectForKey:@"shouldBackup"] isEqualToString:@"Yes"]){
        [defaults setObject:@"No" forKey:@"shouldBackup"];
        [defaults synchronize];

        HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
        [self.navigationController.view addSubview:HUD];

        HUD.delegate = self;
        HUD.labelText = @"Backing Up Your Data..";
        HUD.minSize = CGSizeMake(135.f, 135.f);

        [HUD showWhileExecuting:@selector(performBackup) onTarget:self withObject:nil animated:YES];
    }

However, it appears that viewDidAppear is never called, can anyone explain what it is I'm doing wrong? I've tried several different approaches now and I just can't seem to get it to work..

Thanks,

Tysin

Upvotes: 0

Views: 488

Answers (1)

ElasticThoughts
ElasticThoughts

Reputation: 3477

You need to push the SettingsViewController onto the stack from a NavigationController in order for its delegate methods to be called (in this case viewDidAppear). It seems that your root view controller is a TabBarController. In this case add the UINavigationControllerDelegate to your root VC, more info on what to do next can be found here http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca

Also, are you sure viewDidAppear is not getting called or just the code within it is not getting called? Add a breakpoint to find out.

Lastly, do you have [super viewDidAppear:animated]; in the viewDidAppear method of SettingsViewController?

If all else fails you can always call viewDidAppear manually from the parent view controller :)

Hope this helps!

Upvotes: 1

Related Questions