【问题标题】:Swift 2.0: How to disable current user from pressing button again?Swift 2.0:如何禁止当前用户再次按下按钮?
【发布时间】:2016-03-05 21:10:42
【问题描述】:

我正在尝试在我的应用上实现投票功能。用户必须按下按钮才能投票。用户按下后,不应再按下。每次用户通过 PFRelation 投票(或按下按钮)时,我都会为他们创建一个属性。我还添加了一个禁用按钮方法,该方法会阻止用户执行此操作,除非我的应用程序在每次执行此操作时都会崩溃。我认为这可能与我在 Parse 中构建列的方式有关,但我喜欢听听其他解决方案来做到这一点。我尝试的禁用方法如下。

我创建了一个禁用函数:

func disableButton(button: UIButton){
    button.enabled = false
    button.userInteractionEnabled = false
    button.alpha = 0.5
}

它在我的 voteButton 函数中被禁用:

@IBAction func voteButton(sender: UIButton) {
    disableButton(sender)

    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
     self.userVote?.addObject(object!)
    polls.addObject(object!)
    object!.incrementKey("voteUp")
    object!.saveInBackground()

    self.tableView.reloadData()
    NSLog("Top Index Path \(hitIndex?.row)")
}

这是我的应用不断崩溃的地方:

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?, object: PFObject!) -> PFTableViewCell? {
   let cell = tableView!.dequeueReusableCellWithIdentifier("PollCell", forIndexPath: indexPath!) as! PollsApp

if let userPolls : PFObject = self.polls.objectAtIndex(indexPath!.row) as! PFObject {


if let voteCount = object[("voteUp")] as? Int {
        cell.votersCounts.text = "\(voteCount)"
    }

  if (PFUser.currentUser()?["votedPost"] as! [String]).contains(userPolls.objectId!){
       cell.disableButton(cell. voteButton)
   }

【问题讨论】:

  • 什么是崩溃信息?
  • 您在崩溃报告中看到了什么?
  • 致命错误:在展开可选值时意外发现 nil
  • 我不是 100% 确定如果它是一个关系,你甚至需要这样做 - 我认为 Parse 不允许同一个用户两次填充一个关系......
  • 你认为我应该为 saveInBackground() 调用更好的方法吗?

标签: ios swift parse-platform tableview


【解决方案1】:

我猜当您将 PFUser.currentUser()?["votedPost"] 强制解包为 [String] 时,它会失败,因为它试图强制解包 nil。

改变这个:

if (PFUser.currentUser()?["votedPost"] as! [String]).contains(userPost.objectId!){
   cell.disableButton(cell. voteButton)
}

if let votedPost = PFUser.currentUser()?["votedPost"] as? [String] {
    if votedPost.contains(userPost.objectId!){
       cell.disableButton(cell. voteButton)
    }
}

现在,至于为什么PFUser.currentUser()?["votedPost"] 为零,这是一个不同的问题。

【讨论】:

  • 好吧,我使用的是 PFQueryTableViewController,而 PollApp 是自定义的 tableviewcell。我在那里没有收到错误。我在将 PFUser 写入 tableView 时遇到错误
  • 好的,我看到另一个你强制解包的地方可能失败了
  • 好的,这可以阻止应用程序崩溃。对此感激不尽。但我的问题还涉及重新加载应用程序后禁止用户再次投票。无论如何我可以得到帮助?
猜你喜欢
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
相关资源
最近更新 更多