我已经使用 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/