This is my first post about Swift 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 I'm 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 controllers, I would appreciate it very much.
Thank you in advance.
You may do it by many ways -
If you want to take input string to the immediate next UIViewController, then when you navigate to another UIViewController , initialise two string object of that view from current UIViewController and then navigate. In that UIViewController you can have your string.
If you want to use those strings in some other UIViewController then you can take two string variable in AppDelegate(That is a global class) and set them from current UIViewController and you can use them whenever you want.
If you want to permanent save that data then you can better use NSUserDefaults ( NSUserDefaults - storing and retrieving data )if it is only 2 or 3 or some little amount of field data. If data is more you can use database.
Try these links :
http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/
How do you share data between view controllers and other objects in Swift?
http://blog.apoorvmote.com/how-to-pass-data-via-multiple-segue/
Related
I'm new to Swift and Xcode trying to find my way. I'm learning what I can through struggle, practice, and asking questions. Please excuse my novice level knowledge.
I'm developing an app in which the user will go through a series of questions, each on its own screen containing buttons with different options and sometimes a text field for them to enter a value.
My goal is to be able to use the text field data as variables for one calculation in a final view controller that displays a label with results for the user.
For example, on one view controller, there are two buttons to choose from... one is tapped if they want to know what they need on the final exam ("buttonFinalTakenYes") and another if they want to know what they need on the final before they take it ("buttonFinalTakenNo")...
then no matter what the choice, the next screen asks for their current grade in the class and they will enter a number into a text field and press next...
After a few more questions, depending on what they want to know, I want the final view controller to perform a calculation using all the numbers entered from the text fields leading up to it and display a result as a label. Basically, there are different equations that will display a result based on what the user wants to calculate, and each result will be on a different view controller using data from previous text fields.
I tried searching for answers but I can't seem to figure out what kind of code I need to use for this.
I know that I need to convert the String from the text fields into doubles so they can be used for calculations... but anything beyond that, I'm stuck.
I tried playing around with delegates and segues but it didn't work. Do I need if statements somewhere? Do I need to establish a new protocol? Also, would NSUserDefaults be relevant to this or is it just passing data between view controllers? What if the final view controller is many after the original text field?
Any guidance is appreciated. I feel like I'm making this harder than it needs to be.
Thank you so much!
👋 Glad to see that you are staying positive and asking questions instead of giving up.
I suggest you look up MVC (Model View Controller) because what you are missing in your code currently is the Model part. You need an object that holds all of the data the user entered throughout the app. Each view controller should add it's bit of data to this Model and then pass the model on to the next view controller in the chain.
By the time you are at the last view controller, you will have a Model object that contains all the data the user entered and you can do your calculations on that data and display the result in your last view controller's label.
I'm brand new to Swift but I want to create an app that has a table and each entry in that table will lead to a new screen (but this new screen is the same for all the table entries) but depending on which table cell you click on, that screen has different information posted on it.
What are the steps I need to do to complete this? I have my story board I'm just not sure how to put this all together with code
Thanks in advance!
Well your question is very broad and not very specific. Therefore it is not easy to give a helpful answer. What I would do:
Create a UITableViewController that holds your "table".
Define a UITableViewControllerDelegate for the UITableView that will be informed about UserInteractions (especially when the user
didSelect a certain row of the table).
Based on the specific row (that was selected) you can create a second UIViewController class the shows your intended information.
I can not show some code samples because your question is to broad and things depend on a lot of things (especially on the kind of data you want to show) and how you implement your UIElements on the ViewControllers in InterfaceBuilder.
And if you are not yet familiar with the concept of a UITableViewController and its Delegate, than you should find some tutorials first about that basic technique in developing iOS apps.
You want to use the master/detail pattern. I suggest doing a search on that.
What I basically want is to show the user's name in the first tableView but have a disclosure indicator (segue) to go to a second tableView which will show the history of the selected user. I know how to load data into a table and how to segue from one row to the detail table but I just don't know how to show different data for each user. I know this may be out of my scope of knowledge at the moment but I would like to give it a try.
1- What would be the best way to store the data for each user (history), an array per user?
2- How can I relate data to a specific user so it loads when the user's name is touched?
3- How can I capture the row being selected?
Any suggestions will be appreciated.
Honestly, you shouldn't worry about it being outside of your scope of knowledge, as that's how people learn. You will need to do some amount of work, however, and that part can be a little difficult.
Luckily, what you're trying to do is a very well documented function and Xcode even provides a template for master -> detail views.
Check out this wonderful tutorial by Ray Wenderlich on creating a simple master-detail application: http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1
His other tutorials will also be immensely helpful to beginning iOS development: http://www.raywenderlich.com/tutorials
I'm making a sample app to learn iOS dev, and I have the app create default items to populate the tableview originally, and you can add further ones as well.
If I tap on the default ones (well, there's only one) and segue to the detail view it shows all the details (Name, Location and Date) as intended. But if I add a new one, it comes up with those fields being Empty.
I can't figure out why. I have seemingly all the view refreshing methods set, and it should be setting the detail view's data object which then feeds the labels.
Could anyone help? I'd supply further code if requested, I just don't know what to supply now as I'm not sure exactly where the problem is. It's a rather simple app, though.
Project available here: http://cl.ly/3N0o272M3y1K
Ok this took a bit longer than planned - but it is a good way to learn.
I have attached the project in a zip file.
The code is a bit different then what you posted, but if you compare them side by side, you will see how they are planned and build differently.
It shows the Segue working within the Storyboard.
There where 2 big problems with your project outside of your misuses of the methods.
You need something like Core Data if you want your data to be persistant. In the example I have given you I have used Plist, this will do the trick, but is not persistant in this case.
Also your detailViewController had no labels for the text to appear, so you will need to look into Custom Cells for that - to match your design.
Good Luck and I hope this is useful:-)
Updated Project
I have a TableView that is populated with data. When a user taps a row, a new TableView opens with more data to choose from. This data changes based on the row the user taps. Much like an application where the user chooses from a list of car makes, and then models. Is it possible to handle a situation like that with a TableViewController for the car makes, and 1 TableViewController for the models? Or would there be 1 TableViewController for the makes, and then a separate TableViewController for each make's set of models? I don't know the best way to approach this :-( I'm making an app of my own which is very similar in structure and I can't seem to find any documentation on how to accomplish this. Thanks in advance.
No, your models controller would serve for all makes. It would be passed an ID of the make requested and then fetch the necessary data accordingly.
Something like this might be of some help:
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewAndDataModel/TableViewAndDataModel.html
But I think the foundation of your problem is not necessarily to be found in a working example of your exact needs, but more in general tutorials for ios development.
Sorry if I have assumed you're just beginning ios development. I might be able to improve this answer if a more specific need is clarified (like how do I pass the ID to the model controller) etc
In general - this approach would utilize a separate UITableViewController for each data level and use a UINavigationController to handle the transitions between the different view controllers.
This process used to involve a lot of boilerplate code, but it is now aided by StoryBoards. You can setup each level in a storyboard and handle the transitions and setup data for the next view in performSegueWithIdentifier:.
You can see a tutorial here on using StoryBoards: http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-uitableview-tutorial.html
You can see another Sample From Apple that demonstrates the data drill down with a Storyboard: http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007416-Intro-DontLinkElementID_2