Same View in different view controllers? - ios

I have a view like this:
Each one of the buttons has an action to do when I click on them. And these buttons are changing dynamically through coding (if statements...), when I click some button it leads me to other different buttons values..also the Label is changing accordingly.
ITS LIKE A QUIZ KIND OF THING!
What I want is when I click a button , It should lead me to a different layout of the (view buttons) ( area ).
I was thinking of adding a new view controller for each of what I want to do but this could mean 100 view controllers! I don't know if this is the right way to do it. Is there a better way?

First I'd find similarities between your desired layouts. Implement one view controller making all of those similarities happen. For instance:
show a label on top
show four buttons below
step back and forth between layouts
show different kinds of buttons according to their description
Then you can initialize your view controller with a description of your questions, potential answers/buttons and behavior for each button. Let's say it's a quiz. So you create a class Quiz containing an array of Questions. Each Question contains the question it self as a String and a description for each of the four buttons. It could be the shape and color or a text/answer. And of course some sort of actions for the buttons. It could be just as mutch as a boolean state or a more complicated enum. You get the picture?
(My answer seems to be as vague as your question.)
In the end you'll have to write the whole description for your requirements. I'd write this into a txt, csv, plist, json (whatever is convenient writing and parsing for you) and parse it. That would be the fewest code.

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: First Time Input / Edit Mode, one screen or two screens?

I've create the following screen which takes 4 inputs when the user creates an account and 'Submits' for the first time.
After they submit once, the screen should go to "edit mode'. My question is, is it more effective to create two view controllers (one for initial input, and the other for edit mode), or just one view controller. If the latter, does anyone have any examples or tutorials? I'm positive that I'm not the first one to attempt this. Thanks!
All you want to do here is toggle the button's title and the editability of the text fields, so it is simplest (and best) to do that in place, in the same view and the same view controller. You are merely changing features of this view, not swapping out this view for another world of interface.

Which approach for a form view conditionally showing sets of fields?

I am working on an inventory control application that deals with a few different classes of items. The detail views for each class of item share a mostly the same items on the layout:
Item Name
Photo
Description
Qty in stock
In addition to the basics, different types of items will need to show additional information, like serialized-children or required sub-items etc... In an effort to reduce the number of different layouts I was considering two options:
Option 1:
A tableview with each of these layout items (i.e.: item name, photo etc...) as a cell. Depending on the type of item only the relevant cells would be displayed and populated with data.
As can be seen in the attached screenshot of the FileMaker solution which I am trying to replicate, the fields are stacked vertically, so visually a tableview would work.
Option 2:
Create a single view in Storyboard containing all of the possible fields. When a record is loaded into a view, only the relevant fields would be shown and then slide up to fill in space left by the hidden fields.
Being a newcomer to UI, I am asking for answers to contain the pros and cons of each approach and also encourage other reasonably simple alternative approaches that would achieve the same end-result. Do note that this project is written in Swift and I have no Objective-C experience so any examples are preferably in Swift.
Whenever you are organizing data in this fashion the best approach is Option 1 using a table view. The biggest advantage is the scrollToRowAtIndex function which makes it easy to scroll to a specific text field when it becomes first responder. I'm not sure if you're manipulating data or just displaying it, but this is very helpful:
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
This option also has the advantage of using reusable cells for similar input types, so you only have to create one prototype cell . In addition, if one of your cells has more content, it is easy to resize it to the size of that content.
You stated " When a record is loaded into a view, only the relevant fields would be shown and then slide up to fill in space left by the hidden fields" but this can be applied to a table view as well.

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.

iOS static table in a view?

Sorry if this is a really silly question but I'm new to iOS. I'm trying to do this:
The table cells are static as the content shouldn't change (the options are the same all the time). I also need to place the submit and cancel buttons below and replace the red background with an image.
My problem is, I get the error saying:
static table views are only valid when embedded in uitableviewcontroller
So my question is, how would I embed a table inside my view, like any other element?
Thanks in advance for any help :)
There are two relatively simple options to build a screen like that:
Make your controller a `UITableViewController, add two sections to the bottom of your static part, and put custom cells with buttons into these two sections, or
Define the static content of your table in code.
The look of your screen will change slightly for option 1, but it is important only when you have many cells, forcing the sections with buttons off the screen until the user scrolls.
The second option requires writing some code, but not too much to cause you too much inconvenience.

Resources