gpt4 book ai didi

ios - 处理 UILocalNotification 火灾事件

转载 作者:行者123 更新时间:2023-11-29 03:52:02 26 4
gpt4 key购买 nike

所以我正在使用 UILocalNotification,并且我已经非常熟悉如何

didfinishlaunchingwithoptions

didReceiveLocalNotification

用于在通知触发时处理通知。我可以用执行许多操作的代码来填充这两个函数。

我似乎不明白如何让这些功能与我的应用程序的其余部分“交互”。让我解释一下我的意思。

我最初认为我可以在主视图 Controller 中创建一个委托(delegate)协议(protocol),而不是在这两个函数中执行大部分逻辑。然后,AppDelegate 将注册为委托(delegate),然后我可以让通知事件调用我的 View Controller 类中的方法并在那里执行大部分逻辑。所有这些都按预期进行 - 除了我没有预料到的一种情况。

我已经弄清楚为什么它不起作用了。我在 MyViewController 的 viewDidLoad 函数中分配委托(delegate):

- (void) viewDidLoad
{
[super viewDidLoad];

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

appDelegate.delegate = self;
}

这样做的问题是,当应用程序冷启动时,didfinishlaunchingwithoptions 会在 MyViewController 的 viewDidLoad 函数之前被调用。所以它永远没有机会成为代表。没有机会使用我的委托(delegate)协议(protocol)方法处理我在 didfinishlaunchingwithoptions 中获得的第一个 UILocalNotification。

所以我知道我的问题是什么,我只是想不出更好的方法来处理这些 UILocalNotification 事件。任何想法表示赞赏。

最佳答案

我找到了一个解决方案,但这可能不是一个好的解决方案。

您可以信赖NSUserDefaults .

实现您的didFinishLaunchingWithOptions:,例如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//other code
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"notificationReceived"];
return YES;
}

并且在MyViewControllerviewDidLoad

- (void)viewDidLoad
{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"notificationReceived"])
{
//notification received do the stuff
//reset the bool
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"notificationReceived"];
}
}

关于ios - 处理 UILocalNotification 火灾事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16976225/

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