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 do I display this data as an IBOutlet?
I've written the following function which saves a vote to my database on Parse. That much is complete, and each time a user clicks on the vote button, a vote is saved to the "VoteCount" class. Now, how do I display the number of votes in the class VoteCount as an IBOutlet? Do I need to display it as a println function, and what would it be? Here's my code under the IBAction function:
@IBAction func addVote() {
var voteCount1 = PFObject(className: "VoteCount")
voteCount1["votes"] = 0
voteCount1["optionName"] = "crepes"
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
var query = PFQuery(className: "VoteCount")
query.getObjectInBackgroundWithId("d9FwODwQeh") {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount1["votes"] = 1
voteCount1.incrementKey("VoteCount")
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
}
}
}
}
Hamza Ansari
34 PointsI do now, it is posted below. Do you know where I can write my text and how? I'm totally lost there.
2 Answers
Rutger Farry
10,722 PointsJust make some UILabels and set their text property to be the voteCount. If you want to add text to the label besides just a number, for example instead of saying 72, saying "The number of votes for Crepes is 72", set the text property to:
yourLabelName.text = [[NSString] stringWithFormat:@"The number of votes for Crepes is %d", voteCount1["votes"]];
Hamza Ansari
34 PointsIs that written in Objective-C? My code is in swift. I've created the IBOutlet but I'm not sure how to set the text property, or where to put it. Is that under IBOutlet or IBAction? Here's my updated code:
@IBOutlet weak var votesDisplay1: UILabel!
@IBAction func addVote(sender: AnyObject) {
var voteCount1 = PFObject(className: "VoteCount")
voteCount1["votes"] = 0
voteCount1["optionName"] = "crepes"
var query = PFQuery(className: "VoteCount")
query.getObjectInBackgroundWithId("d9FwODwQeh") {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount1["votes"] = 1
voteCount1.incrementKey("VoteCount")
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
}
}
}
Rutger Farry
10,722 PointsYour IBOutlet, votesDisplay is an instance of UILabel that you laid out in storyboard. So it will be a property of votesDisplay. Check the Apple Documentation of UILabel and figure out what property it is you have to set.
PS: To quickly access the Apple Docs while in Xcode press CMD + Shift + 0.
Hamza Ansari
34 PointsWhen I try to add the votesDisplay1.text under the IBOutlet I just get an Expected declaration error; are you sure the label needs to be the same as the weak variable that the IBOutlet is named as, or does it need to be named something else, like the function addVote in the IBAction? I've also tried self.votesDisplay1.text as well.
Also I don't know if that code is written in Swift, is it?
Rutger Farry
10,722 PointsSorry I only know Objective-C right now. I haven't delved into Swift syntax. You just need to find out how to change the text property using Swift code instead of my objective-c.
Hamza Ansari
34 PointsThat's fine, but thanks for the help!
Rutger Farry
10,722 PointsNo problem! Good luck!
Thorsten Herbst
11,935 PointsThorsten Herbst
11,935 PointsDo you have an IBOutlet?