gpt4 book ai didi

ios - 如何在收到本地通知数据后创建传递给使用 objective-c 的主视图 Controller ?

转载 作者:行者123 更新时间:2023-11-28 21:54:41 24 4
gpt4 key购买 nike

我已经使用 Objective C 为 iPhone 应用程序创建了本地通知流程。我想将 didReceiveLocalNotification 之后的数据传递给主视图 Controller 。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {


if (app.applicationState == UIApplicationStateInactive ) {
NSLog(@"app not running");

// I need to pass the data to main view controller


}else if(app.applicationState == UIApplicationStateActive ) {
NSLog(@"app running");


// I need to pass the data to main view controller

}

NSLog(@"Recieved Notification %@",notif); // Handle the notificaton when the app is running

}

最佳答案

你可以使用 NSNotificationCenter 来传递数据。

MainViewController.m

viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifMainController:) name:@"notifMainController" object:nil];

-(void)notifMainController:(NSNotification *)notif
{
NSLog(@"%@",notif.object);
}

在 AppDelegate 中

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{

NSLog(@"Recieved Notification %@",notif); // Handle the notificaton when the app is running

if (app.applicationState == UIApplicationStateInactive )
{
NSLog(@"app not running");
// I need to pass the data to main view controller
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
MainViewController *obj = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
[self.navC pushViewController:obj animated:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
});

}
else if(app.applicationState == UIApplicationStateActive )
{
NSLog(@"app running");
// I need to pass the data to main view controller
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
}

}

您可以在 [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController"object:@"your string"]; 中传递字符串,就像在对象中这样。也许这会对您有所帮助。

关于ios - 如何在收到本地通知数据后创建传递给使用 objective-c 的主视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965422/

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