gpt4 book ai didi

iphone - iPhone 上收到本地通知时显示 View Controller 时出现问题

转载 作者:行者123 更新时间:2023-11-29 04:26:56 25 4
gpt4 key购买 nike

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

- (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 从未被调用,任何人都可以解释我做错了什么吗?我现在尝试了几种不同的方法,但似乎无法让它发挥作用。

谢谢,

泰氨酸

最佳答案

您需要将 SettingsViewController 从 NavigationController 插入堆栈,以便调用其委托(delegate)方法(在本例中为 viewDidAppear)。看来您的 Root View Controller 是 TabBarController。在这种情况下,将 UINavigationControllerDelegate 添加到您的根 VC,有关下一步操作的更多信息可以在此处找到 http://www.touchthatfruit.com/viewwillappear-and-viewdidappear-not-being-ca

此外,您确定 viewDidAppear 没有被调用,还是只是其中的代码没有被调用?添加断点来查找。

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

如果所有其他方法都失败,您可以随时从父 View Controller 手动调用 viewDidAppear :)

希望这对您有所帮助!

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com