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

- (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,有人可以解释我做错了什么吗?我现在尝试了几种不同的方法,但似乎无法正常工作。

谢谢,

酪蛋白

最佳答案

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

另外,您确定未调用viewDidAppear还是仅未调用其中的代码?添加断点以找出答案。

最后,您是否有[super viewDidAppear:animated];在SettingsViewController的viewDidAppear方法中?

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

希望这可以帮助!

关于iphone - 在iPhone上收到本地通知时显示 View Controller 时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12195136/

10-12 01:25