Swift 2.0: 如何禁止当前用户再次按下按钮?
Swift 2.0: How to disable current user from pressing button again?
我正在尝试在我的应用程序中实现投票功能。用户必须按下按钮才能投票。用户按下它后,他们不应再按下它。每次用户通过 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)
}
我猜当你将 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"]
为零,那是另一个问题。
我正在尝试在我的应用程序中实现投票功能。用户必须按下按钮才能投票。用户按下它后,他们不应再按下它。每次用户通过 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)
}
我猜当你将 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"]
为零,那是另一个问题。