当我收到推送通知时,如何重新加载tableview?

hac*_*ker 7 iphone ipad ios

我有一个iPhone应用程序,我在其中添加推送通知.

当我收到推送通知时,我需要转到特定视图,在此处我在调用Web服务后加载表视图.问题是当我站在同一个观点时.如果我收到推送消息,我需要在前台和后台重新加载tableview.但是,当我这样做时,它无法正常工作.我该如何实现这一目标?

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Mau*_*lik 37

NSNotificationCenter收到推送通知后,您可以尝试使用本地通知重新加载表格.

收到推送通知后,会触发您的本地通知.在视图控制器中,侦听本地通知并执行任务.

例如:

在你的didReceiveRemoteNotification中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
Run Code Online (Sandbox Code Playgroud)

在ViewController中添加Observer(in viewDidLoad):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];
Run Code Online (Sandbox Code Playgroud)

并实现以下方法:

- (void)reloadTable:(NSNotification *)notification
{
    [yourTableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

同时删除观察者viewDidUnloadviewWillDisappear.