如何在事件后通过Parse中获取的数据更改UIButton的文本

时间:2016-01-05 02:44:20

标签: ios swift uibutton

所以我有2个按钮:一个是插座,另一个是动作。我这样做是因为我想改变其中一个的文本,并触发一个增加另一个的计数的函数。当用户点击" like按钮时," like按钮将其增量计数发送给Parse,并应增加喜欢的数量。但出于某种原因,我可以将其新计数(喜欢的数量)发送给Parse,但它不会改变按钮的文本。这是我的代码:

        @IBOutlet weak var likeButton: UIButton!
@IBAction func likeButton(sender: AnyObject) {

    if (parseObject != nil) {
        if var likes: Int? = parseObject!.objectForKey("numberOfLikes") as? Int {
            let newNumberOfLikes = likes! + 1
            parseObject!.setObject(newNumberOfLikes, forKey: "numberOfLikes")
            parseObject!.saveInBackground()

            likeButton.setTitle("\(newNumberOfLikes) likes", forState: UIControlState.Selected)
        }
    }

    // THIS WORKS
    unlikeButton?.hidden = false
    unlikeButton?.alpha = 1.0
    UIView.animateWithDuration(1.0, delay: 0, options: [], animations: {
        self.unlikeButton?.alpha = 1.0
        }, completion: {
            (value: Bool) in self.unlikeButton?.hidden = false
    })

2 个答案:

答案 0 :(得分:0)

由于按钮插座和按钮操作的名称相同( likeButton

尝试更改以下任何一个名称

@IBAction func likeButton(sender: UIButton) {

  sender.setTitle("\(newNumberOfLikes) likes", forState: .Normal)
}

答案 1 :(得分:-1)

尝试更改

__call__

def DecoratorMethod(self, func, *args, **kwargs):
    def wrapped_func(*args, **kwargs):
        with func._lock:
            if not hasattr(func, "counter"):
                func.counter = 0
            print '\nin wrapped func, func name:', func.func_name
            print 'count val:', func.counter
            func.counter += 1
            ret_val = func()
            return ret_val

    return wrapped_func