【问题标题】:iOS - updating view immediately after getting user permissionsiOS - 获得用户权限后立即更新视图
【发布时间】:2023-03-30 01:59:01
【问题描述】:

是否可以在获得用户权限后立即更新视图?

例如,我有一个 tableViewCell 询问用户是否愿意启用推送通知。如果用户按下是,它会打开苹果系统提示以启用推送通知。

我的问题是,有没有办法在用户按下“是”后立即​​更新单元格?

现在,它只是回到 tableView,用户必须手动刷新 tableView 以使“enableNotifications”单元格消失(我让它检查是否已启用推送通知),但我会喜欢在用户在 Apple 的系统对话框中点击“是”后立即​​自动执行此过程

【问题讨论】:

    标签: ios objective-c iphone swift


    【解决方案1】:

    在警报关闭中调用tableView.reloadData(),或者如果您有对单元格索引的引用,您可以只更新该单元格tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

    【讨论】:

      【解决方案2】:

      在iOS8以上,你可以处理UIAlertViewController的每一个动作

      let alertController = UIAlertController(title: "UIAlertController", message: "Do you want to receive notification?", preferredStyle: .Alert)
          let ok = UIAlertAction(title: "Yes", style: .Default, handler: { (action) -> Void in
              print("Yes Button Pressed")
              // do any action here
      
          })
          let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
              print("Cancel Button Pressed")
          }
          alertController.addAction(ok)
          alertController.addAction(cancel)
      

      但在iOS8以下,应该使用UIAlertViewDelegate

      【讨论】:

      • 我会仔细检查,但我认为我们没有使用自己的提示,我相信我们使用的是 Apple 提供的委托方法来检查推送通知。所以我实际上不知道是否有办法改变他们的 UIAlert 上的 yes/cancel 的功能
      • 对不起,我错了,以为这是您的推送自定义设置,但它是苹果推送通知原生提示。你可以参考这里stackoverflow.com/questions/25111644/…
      【解决方案3】:

      只需更改您分配给函数中的单元格的数据,当用户在任何警报上触摸“是”按钮时触发该函数,然后调用此函数。

      [tableView reloadData]; //Objc
      

      tableView.reloadData() //Swift
      

      【讨论】:

        【解决方案4】:

        我不确定,但你可以试试这个。

        在 Appdelegate.m 类中,您可以定义两种推送通知方法

        - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
        // Call you code to update tableview for that ViewController.
        }
        
        - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
        // Show alert to user that you denied
         }
        

        此外,您可以根据您可以管理代码和 UI 以编程方式检查用户是否已授予推送权限。

        对此不确定,但希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 2010-10-27
          • 1970-01-01
          • 2017-01-13
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 2020-05-31
          • 2016-11-09
          • 2019-11-15
          相关资源
          最近更新 更多