Reputation: 535
I'm currently working on an App that uses Parse as backend system. I would like to implement a "Liking" feature. Here's my code
@IBAction func ButtonPressed(sender: AnyObject) {
var query = PFQuery(className: "Content")
query.getObjectInBackgroundWithId(object.objectId, block: { (obj:PFObject!, error:NSError!) -> Void in
if error != nil{
println("Update error \(error.localizedDescription)")
}else{
var likesNumber = obj.objectForKey("Likes")[0] as Int
obj["Likes"] = [likesNumber + 1]
self.likeslab.setTitle("\(likesNumber + 1) likes", forState: UIControlState.Normal)
self.likesButton.enabled = false
obj.save()
}
})
}
The code works well unfortunately, I've a problem: after a user touch on the button I would like to disable it (in order to avoid multiples likes from a same user). I try do that on the following line:
self.likesButton.enabled = false
And It's working but if the user leave the view and then go back, the button is enabled again. Thank you very much for your help !
Upvotes: 0
Views: 2362
Reputation: 15
Swift 3.0
btnEnviar.isEnabled = false
or
btnEnviar.isUserInteractionEnabled = false
Upvotes: 0
Reputation: 1773
You should have to take in some way memory of your button disabled.
One example is to add a flag, in your ViewController, which is updated when you disable or enable your UIButton. So when you update your button with self.likesButton.enabled = false
, you change your flag value to false.
Then in ViewWillAppear
you add these lines:
if flag { self.likesButton.enabled = true } else { self.likesButton.enabled = false }
Obviously that your flag variable should be added as class variable.
Hope that this helps you.
Here's the code that I'd add, for you as beginner:
var isButtonEnabled:Bool = true
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.isButtonEnabled = NSUserDefaults.standardUserDefaults().valueForKey("isButtonEnabled")
if(isButtonEnabled){
self.likesButton.enabled = true
}
else{
self.likesButton.enabled = false
}
}
@IBAction func ButtonPressed(sender: AnyObject) {
var query = PFQuery(className: "Content")
query.getObjectInBackgroundWithId(object.objectId, block: { (obj:PFObject!, error:NSError!) -> Void in
if error != nil{
println("Update error \(error.localizedDescription)")
}else{
var likesNumber = obj.objectForKey("Likes")[0] as Int
obj["Likes"] = [likesNumber + 1]
self.likeslab.setTitle("\(likesNumber + 1) likes", forState: UIControlState.Normal)
self.likesButton.enabled = false
obj.save()
}
self.likesButton.enabled = false
NSUserDefaults.standardUserDefaults().setObject(false, forKey: "isButtonEnabled")
NSUserDefaults.standardUserDefaults().synchronize()
})
}
Upvotes: 0
Reputation: 130102
This is something that has to be provided on the server side. The server has to give you the information whether the user has liked something or not. Then you can set enabled
in viewWillAppear:
method on the controller.
There are some things in your code that smell really bad, for example this:
var likesNumber = obj.objectForKey("Likes")[0] as Int
obj["Likes"] = [likesNumber + 1]
It seems that instead of incrementing the number of likes on the server, you are doing it on the client. There are many reasons why this won't work like you expect it to work.
What you need is a database on the server that will track which user has liked what. You don't want to save just the number of likes, you want to save the list of users who did like the object.
Upvotes: 2