Link a UiLabel with more than one answer - ios

maybe im not asking the right question, or maybe is not what i really need but the thing is in my app I have 2 Viewcontrollers, the first one with a picker view where you can find cities, you chose one and then you press a button that takes you to the second view controller, there you have and other picker view that shows you places according to what city you had chosen, but my problem is that I have a Label (in the second controller) that should show what you choose, so i think i must use different 'ifs' according to the place you have chosen, but it has to depend also on what did you chose on the first view controller and there is my problem, i don't know how to go forward with this.
Hope someone has an answer for that.
Im using Swift.

Sounds to me like you're using a master/detail design. On the first view controller you pick a city. That sends information about the selected city to the second view controller.
In the second view controller you'll look up the data to display for the selected city, and use that city-specific info to populate a picker (and anything else you need.)
You can either store your data for all cities in a global model and simply pass a city index to the second view controller, or you can have the first view controller "peel off" the data for the user's selected city and pass that data to the second view controller.

Related

Using the same text field data for a calculation in different view controllers depending on options selected

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.

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!

Storyboards: How do I use the same view controller in several places?

For years, I've hand-coded my view code for iPhone apps, but now I'm giving storyboards another look.
One common pattern in my code is to use the same view controller in two places, but with slight UI variations. For example, when browsing a list of brands (BrandListController), I'd like to show a table view of all brands in the system; tapping a brand shows you its products. But when filtering items by brand, I'd like to show a table view of brands (with the same content and same cell appearance), but I'd like tapping a row to take you back to the filter screen instead of showing you that brand's items. I'd also like a "Search" bar button item in the top right.
It's easy to make these things happen in code, by just adding a boolean member variable so the controller can be configured before it's presented.
But how would I do this with storyboards? I could have multiple controllers segue to the same instance of BrandListController, but again, I would want the controller to have different behavior and include/exclude UI elements based on how it's used. It seems like a bad idea to create two separate instances of BrandListController in the storyboard, because then I would have to duplicate all the outlet connections and I would have to keep changes in sync. But what are my other options?
The thing to realise with Storyboards is that you don't necessarily have to only use a single storyboard.
You can use multiple storyboards or use them in conjunction with nibs etc...
However, in this case what you could do is still use you boolean property on the controller.
Then in the prepareForSegue method of the other controllers you can set this boolean property to change the behaviour. You might even have a couple of nibs that defines a small section of UI to place into the view depending on the property.
I've done stuff like this to pass blocks into view controller too.
For example...
I had a "User Search" controller that had a default behaviour of if you tap a user it will push to that user's profile page.
But I could pass in a code block that would, for instance, dismiss the search controller and I use the selected user to create a new message to them (or whatever). Or something else entirely depending on the code block I passed in.
EDIT
LOL, just re-read your question. What I did with the blocks would work for you too. I created a block property called userTappedBlock or something. If that exists then I run it when the cell is tapped. If not I do the default behaviour.

Passing data through multiple storyboard segues in Xcode

I have a table view which uses a fetchedResultsController to manage a series of 'Venue' objects from core data. When you select one of the rows on the table view, it passes the current Venue object to a new view (via prepareForSegue), which displays details of the Venue object: name, address, images etc. This all works fine.
I have something I want to achieve but I'm not sure the best or most conventional way to do it. I would like the same behaviour as above, but with a map view sandwiched in between the initial table view and the detail view. So the navigation stack looks like this:
table view (showing all Venues) -> map view (showing all Venues) -> detail view (showing one selected Venue)
Presently I have a button from the table view which segues to the map view, passing an array of annotations which are then plotted on the map. The annotations don't contain all the Venue information, they are just assigned a title and subtitle from each Venue. Each annotation shows a detail disclosure button linking to the detail view. However I am conceptually stuck as to how to pass the relevant Venue object (currently only available in my table view) to the detail view when selected from the map view.
My assumption is that I need to do something along these lines: pass all the Venue objects from my table view to my map view, and just have them existing in the background but not being used. Then when a disclosure button is pressed, the map view checks somehow which Venue matches the selected annotation (checking if the title attribute matches perhaps?) and passes that onto the detail view as the 'selected Venue'. However I don't know how best to do this in practice or even if I have the right idea. Is it possible for my annotation to contain the entire related Venue object? Or for the detail view to somehow ask the initial table view for data?
Hope I'm explaining this clearly enough. Sorry its a bit wordy. I'm still fairly new to Xcode and understand that I may be overlooking something very simple, or making a fundamental error somewhere. Any advice would be much appreciated though!
If you only have one selected object, just pass that on to the map view. The map view can use its own fetched results controller to get whatever other data it needs from the data store (presumably the order of importance could be different from the table view).
You could associated the venue objects with the annotations, but it seems that you only specifically need the one venue to pass to the detail view so it should just be stored as an instance variable.
Create a model where you query a venue by its name/id or something. Then when you go in the last detail view pass the venue name/id to it from the map view.
Or, as you are suggesting in your question, pass the whole venue object to your map view, use whatever you want from this object in this view and when you segue to the detail view pass the whole object again.
you post your data therw -prepareForSegue check the below post
How to pass prepareForSegue: an object
and you can check the below storyboard example
https://github.com/pandamonia/A2StoryboardSegueContext

Maintaining information between viewcontrollers

I am pretty new to stackoverflow so please let me know if there's anything I should add or leave out in my posts from now on.
problem:
I have one original view controller that has a tableview on it with 2 options, "Date" and "hairstyle" the user is able to click on one of these table cells and it brings them to a new view controller where they can select a hairstyle/date. When they click done, they're chosen date/style reflects on the original view controller. The problem is, that it won't reflect both choices at once. Whenever you try to reflect one, the information from the other is reset for some reason.
Feel free to let me know if there is any more information you would like me to share in this post. Thanks for all the help.
Whenever your detail view controllers are about to pop back to the original view controller you can send the data using prepareForSegue.
In order to give you more detailed info we need more details about how you are doing things.
Eagle11 is right you need to use prepareForSegue, before jumping to code you better check this very useful(for me) links:
Passing Data between View Controllers
iphonedevsdk (my personal favourite)
Storyboard tutorial
Passing Data Between VC
Some issues:
passing data between views

Resources