Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hamza Ansari
34 PointsHow to add functions to IBActions in Swift?
Im trying to add a function within this IBAction, the function of which I've called AddVote. What I'm trying to make the button do is add a vote for that button (buttons represent different options for a poll) and I've written out all my code for the functions of adding votes as seen in the code below. But how do I add the functions of adding a vote to the option and displaying the results of both options in a page (there are two buttons hence two options) when the button is pressed? In other words, when someone presses one of the button, I'm trying to make it so that one vote gets added for that button and the results for the entire poll appear after it is pressed. Did I write all the proper functions for those, and how do I add them to the IBAction? Here's my code below:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view,
typically from a nib.
var testObject = PFObject(className:"TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithTarget(nil, selector:
nil)
var voteCount = PFObject(className:"VoteCount")
voteCount["votes"] = 0
voteCount["optionName"] = "Crepes"
voteCount.incrementKey("votes")
voteCount.saveEventually()
var query = PFQuery(className:"VoteCount")
query.getObjectInBackgroundWithId("e8KhneiSfw") {
(voteCount: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount["votes"] = 1
voteCount.incrementKey("votes")
voteCount.saveEventually()
}
}
}
class Counter {
var voteCount: Int = 0
func incrementBy(amount: Int, numberOfTimes times:
Int) { voteCount += amount * times
}
}
override func didReceiveMemoryWarning() {
didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func AddVote() {
}
}