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


How would you create a poll with Swift?

I'm not sure how to create a poll and the corresponding results page in an iOS app with Swift. What type of code would record the number of clicks a certain button gets, and how would I display that information?

1 Answer

Patrick Gerrits
Patrick Gerrits
14,614 Points

Depends on what you want. If you have an app that is used by many people and want to count the number of votes you should probably write some code that when a user clicks the vote button that vote gets transmitted to your server where a scripts runs that will add the vote to the count you have there. Then return the latest count back to the app.

All can be done with an API or some sort.

If it some sort of poll on a forum and you just want to show it in a app, then you can just invoke the api (if there is one) of just call the url which is making the vote.

Thanks for the reply. Your second sentence describes perfectly what I'm trying to do, I just can't think of the function that would record the total number of clicks a certain poll has received or one that returns the total number of votes back to the app for display in the results page. Is there a function in XCode that records the number of clicks?

Patrick Gerrits
Patrick Gerrits
14,614 Points

Dont make it to hard on your self. Just create a local variable called countOption1, countOption2, countOption3, etc.

When user presses the Option 2 vote then execute a simple +=1 code.

countOption2 = 0

--when pressed-- countOption2 += 1

I think you should read into API's. Then create an update API that you can call with options. So for example:


Then that api will update the database for the parameters provided. So it takes option 2 and adds 1 vote. Put some python, php, or whatever you want behind it. So when this update api is called it will run a query an the database.

Thank you! Really appreciate the help!