【问题标题】:How do I reload the tableview when I am getting the push notification?收到推送通知时如何重新加载表格视图?
【发布时间】:2012-07-20 06:45:43
【问题描述】:

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

当我收到推送通知时,我需要在此处调用web service 后转到正在加载表格视图的特定视图。问题是当我站在同一个视图中时。如果我收到推送消息,我需要在前台和后台重新加载 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;
    }
}

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    您可以尝试使用本地通知NSNotificationCenter 在收到推送通知时重新加载您的表格。

    当收到推送通知时,触发您的本地通知。在您的视图控制器中,监听本地通知并执行任务。

    例如:

    在您的 didReceiveRemoteNotification 中:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
    

    在你的 ViewController 中添加观察者(viewDidLoad):

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

    并实现以下方法:

    - (void)reloadTable:(NSNotification *)notification
    {
        [yourTableView reloadData];
    }
    

    同时删除viewDidUnloadviewWillDisappear 中的观察者。

    【讨论】:

    • 效果很好,我实际上登录 StackOverflow 只是为了给你投票。从来不知道 iOS 中的 Observers,谢谢
    • 非常感谢!!!!当收到远程通知并且应用程序处于前台时,我使用它来显示自定义通知。你拯救了我的一天:D +1!
    • 难道你也不需要在 viewWillDisappear 中移除 Observer 吗?
    • @carmine:是的,真的..你应该..这只是一个想法。无论如何,我已经编辑了答案,所以它可能会有所帮助。谢谢
    【解决方案2】:

    Swift 3 版本:

    在 didReceiveRemoteNotification 中

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTheTable"), object: nil)
    

    在您的 ViewController 中添加观察者(在 viewDidLoad 中):

    NotificationCenter.default.addObserver(self, selector: #selector(self.reloadTable), name: NSNotification.Name(rawValue: "reloadTheTable"), object: nil)
    

    在视图中WillDissapear

     NotificationCenter.default.removeObserver("reloadTheTable")
    

    【讨论】:

      【解决方案3】:

      Swift 4 版本: 在 didReceiveRemoteNotification 中

      NotificationCenter.default.post(name: Notification.Name(rawValue: "reloadEventsTable"), object: nil)
      

      在您的 ViewController 中添加观察者(在 viewDidLoad 中):

      NotificationCenter.default.addObserver(self, selector: #selector(onReloadEventsTable), name: Notification.Name(rawValue: "reloadEventsTable"), object: nil)
      

      关于删除观察者我曾经在 deinit() 中执行此操作,但有趣的是,从 iOS 9 开始,如果您不使用基于块的观察者,您不需要自己删除观察者。 https://stackoverflow.com/a/40339926/3793165

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 2011-10-14
        相关资源
        最近更新 更多