Taking user input and displaying it in multiple view controllers?

Hi all,


This is my first post and I hope someone can help me. I'm a junior Ruby/Rails developer and have recently started building an app for a personal project to get to grips with developing in Swift and XCode, as it is something I am interested in moving into.


I have just started building my app, but have hit a problem almost straight away that I have been unable to find a clear answer to or any guidelines that I can bend to serve my purpose. I'm sure it must be a fairly simple issue to solve, so I'll try and write my problem as clearly as possible:


If I want to take some user input from a text field, such as a name or a location, and then display that input in another view controller, how would I go about doing it?


So far in my app, a user is presented with a button, which when pressed, shows a view controller with two text fields. I want to take the input strings from the two text fields and display that data in a label on the next view controller, so the user can add more information to the object before publishing it to be displayed in a table view cell elsewhere in the app. I have used IBOutlets on the text fields.


I have been reading about Core Data in Swift and have worked through a couple of tutorials about adding data to a table view, including the Start Developing iOS Apps (Swift) from Apple and I think Im on the right track to finding out how to achieving the functionality I want. But I'm not entirely sure, and wanted to ask if I am on the right path, or if I'm going about it the wrong way?


If anyone could link me to any tutorials or guides or point me in the right direction so that I can figure out what seems like a basic step of storing data that can be retrieved and displayed in other view contorllers, I would appreciate it very much.


Thank you in advance, Frazer.

If I want to take some user input from a text field, such as a name or a location, and then display that input in another view controller, how would I go about doing it?

The basic strategy here is:

  1. when you set up your first view controller, give it a connection to the relevant model objects

  2. have your first view controller store its data there

  3. when you set up your second view controller, give it a connection to those same model objects

  4. have your second view controller get its data there

If there’s a parent/child relationship between the first and second view controllers, you can accomplish step 3 in the first view controller’s

prepareForSegue(_:sender:)
method.

If both view controllers can be on screen at the same time, you’ll need some sort of notification mechanism so that the view controller hear about the model has been updated. The usual suspects here are NSNotification and KVO. If you’re just getting started, I’d probably shoot for NSNotification.

You can use Core Data for your model, but it’s not required. Core Data is great if you want to persist a complex object graph, but for simple stuff it’s fine to use custom in-memory model objects and then persist them using a property list, keyed archive, or whatever.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Taking user input and displaying it in multiple view controllers?
 
 
Q