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
Alexander Certosimo
1,774 PointsIs user input in an app covered?
Hey all,
So i just realized that i have not seen anything on getting user input within an app yet. I am curious about this and what would be a good approach specifically if i wanted to get user inputted data and then store to be able to use it later? Could i store it into an array to then be randomly shown in view everytime a button is pressed? Kind of like the fun facts app?
thanks for your time
2 Answers
Gabe Nadel
Treehouse Guest TeacherSo, there are lots of ways to store user input, and which route you go depends on afew things:
What kind of data: (big like audio, small like text, sensitive like passwords, etc...)? How will it be used: does it need to be connected to other data, like through a relational database? When will it be used: will it persist from one session to the next.
Assuming you do want it to persist from one session to the next, you could see Core Data as the far more robust (and complex) side of that spectrum, for simpler cases, like storing a small array of user preferences, you can start with NSUserDefaults (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/)
If you've never used NSUserDefaults, it's definitely worth trying it out. It's pretty simple to implement and comes in handy in all sorts of spots, everything from persisting In-App-Purchases feature unlocks, to remembering user settings, to recording where users left off in the app, to tracking small data sets.
Gabe Nadel
Treehouse Guest TeacherTo get the input try using a UITextview. You'll of course want to create an IBoutlet for it, too, and hook that up. The. You can create a string from the UITextView.text and save that string to a variables which you can display in your UILabel. You may not even need NSUserDefaults, unless you want the text persist.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 Pointshi Gabe
Thanks for the reply. I am going to look into the info you provided me. Basically what i am trying to do is get a user inputted sentence, store it, then when a button is pressed have that user inputted sentence come up in a label. It seems pretty simple i have just been struggling with really understanding the user input stuff for swift