在iPhone上收到本地通知时显示视图控制器的问题

时间:2012-08-30 10:46:26

标签: iphone objective-c ios uiviewcontroller uilocalnotification

以下代码是我在应用程序收到特定本地通知时显示视图控制器的最新尝试:

- (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);
    }
}

然后我在viewDidAppear方法中使用这段代码来确定是否执行备份:

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];
    }

然而,似乎从未调用viewDidAppear,任何人都可以解释它是什么我做错了吗?我现在尝试了几种不同的方法,但我似乎无法让它起作用..

谢谢,

Tysin

1 个答案:

答案 0 :(得分:1)

您需要将SettingsViewController从NavigationController推送到堆栈,以便调用其委托方法(在本例中为viewDidAppear)。您的根视图控制器似乎是一个TabBarController。在这种情况下,将UINavigationControllerDelegate添加到根VC,可以在此处找到有关下一步操作的更多信息http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca

另外,你确定viewDidAppear没有被调用或只是其中的代码没有被调用吗?添加断点以查找。

最后,你有[super viewDidAppear:animated];在SettingsViewController的viewDidAppear方法?

如果所有其他方法都失败了,您始终可以从父视图控制器手动调用viewDidAppear:)

希望这有帮助!