当我收到推送通知时,如何重新加载tableview? [英] How do I reload the tableview when I am getting the push notification?
问题描述
我有一个iPhone应用程序,我在其中添加推送通知。
I have an iPhone application in which I am adding push notifications.
当我收到推送通知时,我需要转到我正在加载的特定视图在此处调用网络服务后的表格视图。问题是当我站在同一个观点时。如果我收到推送消息,我需要在前台和后台重新加载tableview。但是,当我这样做时,它无法正常工作。我如何实现这一目标?
When I am receiving the push notification I need to go to a particular view where I am loading a table view after calling a web service here. The problem is when I am standing in the same view. If I got a push message I need to reload the tableview both in the foreground and background. But when I am doing that it is not working correctly. How do I achieve this?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"########################################didReceiveRemoteNotification****************************###################### %@",userInfo);
// Check application in forground or background
if (application.applicationState == UIApplicationStateActive)
{
if (tabBarController.selectedIndex==0)
{
NSArray *mycontrollers = self.tabBarController.viewControllers;
NSLog(@"%@",mycontrollers);
[[mycontrollers objectAtIndex:0] viewWillAppear:YES];
mycontrollers = nil;
}
tabBarController.selectedIndex = 0;
//NSLog(@"FOreGround");
//////NSLog(@"and Showing %@",userInfo)
}
else {
tabBarController.selectedIndex = 0;
}
}
推荐答案
你可以尝试使用本地通知 NSNotificationCenter
在收到推送通知时重新加载您的表格。
You can try with the local notification NSNotificationCenter
to reload your table when a push notification is received.
推送通知时收到然后解雇您的本地通知。在视图控制器中,收听本地通知并执行任务。
When a push notification is received then fire your local notification. In your view controller, listen to the local notification and perform the task.
例如:
在您的视图中didReceiveRemoteNotification:
In your didReceiveRemoteNotification:
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
在ViewController中添加观察者(在 viewDidLoad
中):
In your ViewController add Observer (in viewDidLoad
):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];
并实施以下方法:
- (void)reloadTable:(NSNotification *)notification
{
[yourTableView reloadData];
}
同时删除 viewDidUnload $ c $中的观察者c>或
viewWillDisappear
。
这篇关于当我收到推送通知时,如何重新加载tableview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!