2015-12-01 76 views
1

我试图在我的应用上实现投票功能。用户必须按下一个按钮才能投票。用户按下后,他们不应该再次按下它。每次他们通过PFRelation投票(或按下按钮)时,我为用户创建了一个属性。我还添加了一个禁用按钮方法,可以阻止用户这样做,除非我的应用程序每次崩溃。我认为这可能与我在Parse中构造列的方式有关,但我喜欢听其他解决方案来实现此目的。下面是我尝试的禁用方法。Swift 2.0:如何禁止当前用户再次按下按钮?

我已经创建了一个禁用功能:

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) 
    } 
+2

什么是崩溃消息? – alinoz

+2

你在碰撞报告中看到了什么? – Peyman

+0

致命错误:意外地发现零,而展开一个可选值 – Bachenenad

回答

2

我猜测,当你强制拆包PFUser.currentUser()?["votedPost"]作为[String]它是失败的bec因为它试图强制解包零。

更改此:

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"]是零,这是一个不同的问题。

+0

那么我使用PFQueryTableViewController和PollApp是自定义tableviewcell。尽管如此,我并没有发现错误。我得到的错误,我PFUser写在桌子里面查看 – Bachenenad

+0

好的,我看到另一个地方,你是强行解开,可能是失败 –

+0

好吧,停止应用程序崩溃。对此感激不尽。但我的问题还涉及禁用用户重新加载应用程序后再次投票。无论如何,我可以得到帮助吗? – Bachenenad

相关问题