2015-12-01 75 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

相關問題