Multiple TableViewControllers & Segues - ios

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

Related

How to create a table in swift with segue?

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.

Taking user input and displaying it in multiple view controllers in Swift?

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/

Swift iOS solution for multiple/relational drop down pickers

I am new to programming iOS and I am not sure on how to implement multiple/relational drop down pickers into my design.
I have a search form in my app that works like a panel-menu. If you click on the search icon then the panel slides in with the search form.
But what is the best way to implement multiple/relational drop down pickers for my search form?
The pickers are relational. First you select a state then you must select a city. Once you selected a state in dropdown1 then dropdown2 should get populated depending on what you selected in dropdown1.
So is there any good solution for this when it comes to design?
I would like to show both pickers at all time. Kinda like a datepicker when year / month / day always is shown.
But if anyone has a good resource example on relational pickers please share.
Thanks in advance,
You can set this up with nested UITableViewControllers.
These types of projects are generally called Master/Detail.
The Master tableView would display the list of categories.
Once the user has selected a category, the specific category is passed to the detail View controller. It queries all the subcategories for that category.
This can all by done in Storyboard, using Auto Layout, self-sizing cells, and a combination of show and unwind segues.
A show segue pushes a (table) view controller onto the navigation controller stack. In your case, the category controller would push a subcategory controller. prepareForSegue:sender: is where the category controller would provide the category to the subcategory controller.
An unwind segue returns from a view controller, popping it off the navigation stack. In your case, the subcategory controller would return (with the selected subcategory information) to the category controller, or a previous view controller.
It may sound like a lot to digest, but if you read up on recent (i.e. for iOS 8) walkthroughs which use these concepts, you'll have learned some acceptable practices for how information and control should flow within an app.
There's one more thing I didn't mention, called Core Data. Core Data, and NSFetchedResultsController would be a great tool to learn and use for the app. It's probably more complex than anything I previously mentioned, but once you get a handle on it, you will really appreciate it, and may end up using it in many apps!
Don't get too bogged down with how your app should look. Focus on how the model and view controllers are written, and get a good understanding of the underlying frameworks. That's more important right now, than any fancy transition/animation.
The design of any app will evolve as you use it. You'll discover what works well, and what doesn't, so don't get too attached to any one way of organizing the data!
Hope that helps! Enjoy programming for iOS, it's a great platform!

How to relate data to a specific row in a UITableView

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

Trouble understanding viewControllers in iOS

I'm new to obj-c/iOS and I'm having trouble understanding conceptually at least viewControllers. I've read a lot of the Apple Doc's, I've even used viewControllers to some extent in xCode, but I still don't quite get what they are, or what are the best ways to use them.
I've been an AS3 dev for many years so my mind works in the context of MovieClips/Sprites and the Display list to get graphics on the screen.
Ok so from my understanding...
A viewController is a kind of class that handles graphics in some
fashion and then allows you to do something with them?? What is it in it's most basic sense?
You seem to add viewControllers to a Window class, which I guess is a bit like
adding a display Object to the Display list?
What is it that a viewController does for you in it's most basic sense?
Are there certain things you definitely can't do with them or shouldn't do
with them?
Do viewControllers need to be connected in some way to the rest of the iOS framework to function (apart from being added to a window).
How exactly do they use data? (I've read up on MVC, I understand that conceptually this is a slightly different question) as I understand it you don't hardcode data into a viewController, so how does a viewController access any static data?
Let's say I just wanted to throw an image up on the screen, exactly what part would the viewController play in that process? is it just something which handles only one small aspect of that process or is it the whole show and handles everything?
Does one viewController handle multiple images? is it like it's term, a "controller" for all the images presented on screen, or does it handle one image at a time?
What is a viewControllers connection to the image(s) it handles? it contains references to them?
I'm using the Sparrow framework which is helping but I would still like to be able to get my head around what viewControllers are so I know how to use them properly.
Ha, I apologise for the above I know it must look like I'm completely confused :) thanks for any advice.
Hope this helps you:
A viewController is a kind of class that handles graphics in some fashion and then allows you to do something with them??
It's the glue between a View (Xib File) and the Data (Could be
CoreData or whatever you're using in the backend). All the UI Elements
you are using in the View you normally define as properties in the
controller to get access to them.
What is it in it's most basic sense?
You seem to add viewControllers to a Window class, which I guess is a bit like adding a display Object to the Display list?
I don't really know AS3 so I cannot compare Display lists with ViewControllers. But basically ViewControllers are there to handle
different types of transitions between the views and accessing
(setting/reading) the data which is displayed in the view.
What is it that a viewController does for you in it's most basic sense?
Like I've written above. Most basic sense they interpret what the user
does on the view and depending on the action of the user changes the
model.
Are there certain things you definitely can't do with them or shouldn't do with them?
It is always hard to keep the border between model and controller.
They are pretty close to each other. So what I normally try is to
delocate all logic stuff (like calculations, database access and so
on) this does more belong into the model part. But of couse you're
using these external classes in the controller.
Do viewControllers need to be connected in some way to the rest of the iOS framework to function (apart from being added to a window).
Well like you already have written the ViewController needs to be
connected to a view. Otherwise it would not make much sense. There are
different subtypes of UIViewController such as UINavigationController
where you probably need to overwrite some other methods to provide the
whole functionality wanted by these special subtypes.
How exactly do they use data? (I've read up on MVC, I understand that conceptually this is a slightly different question) as I understand it you don't hardcode data into a viewController, so how does a viewController access any static data?
There could be different approaches to store the data. Simplest way
would be to have the data directly stored in the UIViewController.
This could be a custom class which is the container of the data. All
changes are directly written into this class and displayed by the
UIViewController. But in most of the cases it makes sense to use
CoreData (Which is responsible for reading/writing the data into a
sqlite database). You could look at CoreData as your model and the
UIViewController gets the data from there and passes the data which
the UIViewController has received from the View back to it.
Let's say I just wanted to throw an image up on the screen, exactly what part would the viewController play in that process? is it just something which handles only one small aspect of that process or is it the whole show and handles everything?
The UIViewController would store an internal Property (UIImageView *)
which is in the Interface Builder connected with the UIImageView you
have created in the Xib file. So over these property you can change
through your Controller the image.
Does one viewController handle multiple images? is it like it's term, a "controller" for all the images presented on screen, or does it handle one image at a time?
Yes, this isn't a big problem. You can have as many images you want.
You just need to have the properties defined in the UIViewController
and linked to the View.
What is a viewControllers connection to the image(s) it handles? it contains references to them?
Yeah, its like a reference to the UIElement. You can then change
whatever property of the UIImageView you want directly from the
UIViewController
Some useful links:
Apple Official ViewController Guide
Apple Official ViewController Basics
You should have a look at Storyboards (U can use them since IOS 5.0)
I recommend you to check:
https://stackoverflow.com/questions/1939/how-to-articles-for-iphone-development-and-objective-c
Here are the answers to your questions:
No, it's doesn't handle graphics. It's the controller of the MVC design pattern. It handles the lifecycle of it's contents (for instance the views) and the data linked with.
A UIViewController is set as a root of an UIWindow. For instance, a UINavigationController is a subclass of UIViewController that stacks UIViewController in order to deal with the navigation.
Response in (1)
Try to be more specific with this question please.
As already commented, it's useful if you use the already built-in components like UINavigationController or UITabBarController.
For instance, you can have the data in instance variables and the display them in the contained UIView.
The UIView attached to your UIViewController will contain an UIImageView. Your UIViewController would have a connection with it in order to whatever changes you need, for instance, changing the image when the user press a button.
It can contain multiple UIViewsand therefore multiple UIImageViews (it's a subclass of UIView)
As commented, they would be contained on an UIImageView and would be linked programmatically or with an IBOutlet.
In a nutshell, a view controller is the controller in the MVC pattern. Please check this link before reading further so you're up to date with this pattern:
http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
OK, basically a controller manages a collection of views. It also fetches data from your model and sets the state of the views. It's important to note that the views know nothing of your model (your data), and the model knows nothing about your views. A controller also receives events from the views and decides how to change your model accordingly. It is essentially managing the synchronisation between your views and model.
There are technologies that help automate this such as KVO and key value binding. A google search will help you there.
One more thing. No other part of your application should access your views except for the controller. So generally in an application controllers tend to communicate with each other, for example via transitions or the delegate patterns between controllers under a navigation controller. So your application backbone tends to be controllers talking to each other.

Resources