Reputation: 377
I have a button as seen in the image and when the user presses it first it should change the image and the user is now following this profile. However when the user presses the button first it does nothing and on the second tap it works. How can I make this work on the first tap?
Edit cleaned up code This code takes two taps for the button to work. With a double tap it follows or unfollows the user and updates the image. But like i said it takes two taps.
@IBAction func favButton(sender: UIButton) {
buttonPressed = !buttonPressed
if buttonPressed == true {
let followedObjectId = userToShowDetail?["name"] as? String
if isFollowing[followedObjectId!] == true {
}
} else {
let followedObjectId = userToShowDetail?["name"] as? String
if isFollowing[followedObjectId!] == false {
isFollowing[followedObjectId!] = true
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
print("follow")
let following = PFObject(className: "Followers")
following["following"] = userToShowDetail?["name"] as? String
following["follower"] = PFUser.currentUser()?.objectId
favLabel.text = "Favourite Jammer"
following.saveInBackground()
} else {
if buttonPressed != true {
isFollowing[followedObjectId!] = false
print("notFollow")
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
favLabel.text = "Add to favourites?"
}
}
}
This code fixed it visually, but it no longer works behind the scenes. The button changes image and prints follow and unfollow to the logs but it doesn't unfollow a user and also adds the followed user to my data twice.
buttonPressed = !buttonPressed
if buttonPressed == true {
let followedObjectId = userToShowDetail?["name"] as? String
isFollowing[followedObjectId!] = true
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
print("follow")
let following = PFObject(className: "Followers")
following["following"] = userToShowDetail?["name"] as? String
following["follower"] = PFUser.currentUser()?.objectId
favLabel.text = "Favourite Jammer"
following.saveInBackground()
} else {
if buttonPressed != true {
let followedObjectId = userToShowDetail?["name"] as? String
isFollowing[followedObjectId!] = false
print("notFollow")
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
favLabel.text = "Add to favourites?"
} // !=true
} // else
} // uibutton
Upvotes: 0
Views: 493
Reputation: 2226
The code is a very messy and I wouldn't recommend going about it like this... Too many queries are happening and too much in general is going on in that button...
However, any query (data the you want to access and modify in your app) should be in the viewDidLoad
(or a similar method) so that the values are there before the button is pressed. These values should be stored in arrays. And when the button is pressed, you will already have access to the values and it'll be easier to modify them in the future and in different methods.
This needs to be shortened.
But, assuming you still want to do it this way the button logic needs to be broken down into functions.
Create a function out of that query and then reference it in the button. Too many things are happening + you're calling the button in the wrong place.
This is a very rough example:
Your button should look something like this:
@IBAction func favButton(sender: UIButton) {
buttonPressed = !buttonPressed
if buttonPressed == true {
let followedObjectId = userToShowDetail?["name"] as? String
if isFollowing[followedObjectId!] == true {
//something needs to happen here?
}
updateImage()
} else {
followersUpdate()
}
}
//Your functions below the button:
func followersUpdate() {
let followedObjectId = userToShowDetail?["name"] as? String
if isFollowing[followedObjectId!] == false {
isFollowing[followedObjectId!] = true
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
print("follow")
let following = PFObject(className: "Followers")
following["following"] = userToShowDetail?["name"] as? String
following["follower"] = PFUser.currentUser()?.objectId
favLabel.text = "Favourite Jammer"
following.saveInBackground()
} else {
}
}
func updateImage() {
if buttonPressed != true {
isFollowing[followedObjectId!] = false
print("notFollow")
sender.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
favLabel.text = "Add to favourites?"
let query = PFQuery(className: "Followers")
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
query.whereKey("following", equalTo: (userToShowDetail?["name"] as? String)!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
object.deleteInBackground()
}
}
})
}
}
//Your viewDidLoad() initial query to parse:
override func viewDidLoad() {
super.viewDidLoad()
let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
for object in users {
if let user = object as? PFUser {
if user.objectId! != PFUser.currentUser()?.objectId {
self.usernames.append(user.username!)
self.userids.append(user.objectId!)
let query = PFQuery(className: "Followers")
query.whereKey("follower", equalTo: (PFUser.currentUser()!.objectId)!)
query.whereKey("following", equalTo: user.objectId!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
if objects.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
}
})
}
}
}
}
})
}
As I mentioned, this is a rough example to give you an idea of what you should be doing (even though I think the queries and server sending back and forth is 1) superfluous, 2) not needed and 3) will make your app slower.)
If you break it down like this and have less going on in your button, the image should update. You're going to have to play around with this and revise the code/make it much shorter.
Let me know if you have more questions I'll be happy to help.
Upvotes: 2