2015-10-27 16 views
-3

我在做一个应用程序,用户可以发布/评论/和喜欢的职位。我想实施类似于YouTube的赞成/反对。我currenlty有两个IBActions喜欢和不喜欢的按钮。我的问题是,如何在选择另一个按钮时取消选择按钮并减小/增加每个按钮的值?反之亦然?这是下面的代码。大拇指向下 - YouTube的风格投票系统,其中用户只能投票一次,但可以从喜欢变成喜欢反之亦然

@IBAction func dislikeButton(sender: UIButton) { 



    let selected: Bool = !sender.selected 

    var likeCount: Int = Int((sender.titleLabel?.text)!)! 

    if selected { 
     //upvote 
     likeCount++ 
     print("inc \(likeCount)") 

    } else { 
     //downvote, but do not allow negative values 
     if likeCount == 0{ 
      likeCount = 0 
     } else { 
      likeCount-- 
     } 
     print("dec \(likeCount)") 
    } 
    sender.setTitle(String(likeCount), forState: UIControlState.Normal) 
    sender.selected = !sender.selected 


} 





@IBAction func likeButton(sender: UIButton) { 

    let selected: Bool = !sender.selected 

    var dislikeCount: Int = Int((sender.titleLabel?.text)!)! 

    if selected { 

     //upvote 
     dislikeCount++ 
     print("inc \(dislikeCount)") 

    } else { 
     //downvote, but do not allow negative values 
     if dislikeCount == 0{ 
      dislikeCount = 0 
     } else { 
      dislikeCount-- 
     } 
     print("dec \(dislikeCount)") 
    } 
    sender.setTitle(String(dislikeCount), forState: UIControlState.Normal) 
    sender.selected = !sender.selected 

} 

回答

1

它通常是更好的设计到模型从视图中分离出来。也就是说,更好的设计(在你的情况下)存储用户的投票(像/ dislike/none),而投票直接计数,而不是隐式地作为按钮状态和标签。

class VotableItem { 

    // Votes: -1 for dislike, 1 for like, 0 for no vote 
    var vote: Int = 0 // initialize to user's existing vote, retrieved from the server 
    var likeCount: Int = 0 // initialize to existing like count, retrieved from the server 
    var dislikeCount: Int = 0 // initialize to existing dislike count, retrieved from the server 

您还需要连接到喜欢和讨厌按钮网点,更新它们:

@IBOutlet var likeButton: UIButton! 
    @IBOutlet var dislikeButton: UIButton! 

当用户点击了Like按钮,有两种可能的行动。如果现有的投票是“喜欢”(存储为1),她想撤消她的“喜欢”投票并将其投票回复为“无”(存储为0)。否则,现有的投票是“无”或“不喜欢”(存储为-1),她想撤消她现有的投票并将其投票为“喜欢”。

类似地,当用户点击按钮不喜欢,有两种可能的动作。如果她现有的投票“不喜欢”,她想撤消它,将其设置为“无”。否则,她想撤销她现有的“喜欢”或“无”投票,并将她的投票设定为“不喜欢”。

注意,撤消“无”投票没有任何影响,但逻辑是简单的,如果我们允许撤消它(什么都不做)。

由于喜欢和讨厌按钮的动作是如此的相似,我们可以分解出共同的行为。按钮连接到这些操作:

@IBAction func dislikeButtonWasClicked(sender: UIButton) { 
     buttonWasClickedForVote(-1) 
    } 

    @IBAction func likeButtonWasClicked(sender: UIButton) { 
     buttonWasClickedForVote(1) 
    } 

然后实现buttonWasClickedForVote(_:)功能:

private func buttonWasClickedForVote(buttonVote: Int) { 
     if buttonVote == vote { 
      // User wants to undo her existing vote. 
      applyChange(-1, toCountForVote: vote) 
      vote = 0 
     } 

     else { 
      // User wants to force vote to toggledVote. 
      // Undo current vote, if any. 
      applyChange(-1, toCountForVote: vote) 
      // Apply new vote. 
      vote = buttonVote 
      applyChange(1, toCountForVote: vote) 
     } 

     updateUserInterfaceFromModel() 
    } 

我使用一个辅助函数来更新计数。需要注意的是它什么都不做,如果票为0(意为“无”):

private func applyChange(change: Int, toCountForVote vote: Int) { 
     if vote == -1 { dislikeCount += change } 
     else if vote == 1 { likeCount += change } 
    } 

我们需要另一种功能,使用户界面状态的匹配模型:

private func updateUserInterfaceFromModel() { 
     likeButton.selected = vote == 1 
     likeButton.setTitle("\(likeCount)", forState: .Normal) 
     dislikeButton.selected = vote == -1 
     dislikeButton.setTitle("\(dislikeCount)", forState: .Normal) 
    } 

注意,updateUserInterfaceFromModel功能不依赖于用户界面的当前状态。这意味着您也可以在初始化过程中使用它。只需设置vote,likeCountdislikeCount(推测基于服务器的数据)。然后,在viewDidLoad中,请拨打updateUserInterfaceFromModel

+0

嘿真的很感谢答案!我如何将它存储在Parse数据库中? –

+0

您需要阅读Parse文档,也可以按照Parse教程来学习API。 –

+0

谢谢。我尝试了你的代码,但在这里得到一个错误:最后一个函数:func updateUserInterfaceFrommodel()..即时通讯获取致命错误:意外发现零将解开一个可选值...帮助表示赞赏! –