我有一个应用程序,如果用户有一段时间没有打开该应用程序,我将在其中使用本地通知。
如果用户通过本地通知打开应用程序,我的数据库会更新并为用户帐户提供积分,并且我会显示“您已获得额外积分”的警报。
我的数据库中的信用更新当用户单击“确定”时,但我的 View Controller 没有刷新,并且当时没有在标签上显示更新的信用。
当我转到另一个 View 并返回时,它显示了。
当用户从 AlertView 单击“确定”时,我应该如何获取更新的分数?
提前致谢....
编辑
[[UIApplication sharedApplication]cancelAllLocalNotifications];
NSDate *today=[NSDate date];
NSLog(@"%@",today);
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"dd-MMM-yyyy hh:mm:ss a"];
NSTimeInterval secondsInEightHours = 20;
NSString *tt=[dateFormat1 stringFromDate:today];
//tt=@"20-Apr-2013 06:39:32 PM";
NSDate *Ass_date=[dateFormat1 dateFromString:tt];
Ass_date=[Ass_date dateByAddingTimeInterval:secondsInEightHours];
NSLog(@"%@",tt);
NSLog(@"%@",today);
NSLog(@"%@",Ass_date);
//[[UIApplication sharedApplication]cancelAllLocalNotifications];
UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication]cancelLocalNotification:localNotif1];
if (localNotif1 == nil)
return;
localNotif1.fireDate = Ass_date;
localNotif1.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif1.alertBody = @"You have not played for a long time. Play and get 250 Stars credits for free!";
// Set the action button
localNotif1.alertAction = @"Get It Now";
localNotif1.soundName = UILocalNotificationDefaultSoundName;
//localNotif.applicationIconBadgeNumber = 1;
//localNotif1.applicationIconBadgeNumber ++;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
[localNotif1 release];
这是我在 viewcontroller 的 ViewDidAppear 上做的代码。
现在在 application didReceiveLocalNotification: 的 app delegate 中,我显示了一个警报。
[self localNotif];
////NSLog(@"Recieved Notification %@",localNotif);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"250 Stars have been credited to your account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
单击警报中的“确定”按钮后,我希望我的 View Controller 能够刷新或重新加载。
最佳答案
从更新学分的任何地方发送一个 NSNotification
(与本地通知不同)。让您的 UIViewController
注册该通知,并更新其显示。
标准通知示例,发布:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppCreditsUpdatedNotification" object:nil];
接收(这进入你的 View Controller ):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCredits:) name:@"MyAppCreditsUpdatedNotification" object:nil];
不要忘记将其放入您的 dealloc 中:
[[NSNotificationCenter defaultCenter] removeObserver:self];
关于iphone - 本地通知在 ios 中运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833198/