Present different outlets on the same view controller through navigation controller? - ios

I want to present different outlets to the user for different stages of a registration form. For example they enter their first name and last name, press continue and then go onto the next part of the form to enter their date of birth etc and at each stage of the form I want it to be part of a navigation view controller so they can easily press back to go back to the previous part and perhaps change it. Just like how snapchat does when you sign up.
I want only one view controller which will manage all the validation and outlets from the various parts of the form.
I tried creating multiple of the same view controller, and putting different text fields and buttons etc on each page. They were then all embedded in a navigation view controller with segues to each part of the form. The problem with using the same view controller is every time viewDidLoad is called it re-initialises the outlets so ones the user previously entered become nil. I dont want to just save the textfields to a struct for example because the user can press back to go back to this field (and i dont want them to then have to re-enter the information). There must be a simple way to do this?
Also I dont want to create a different view controller for every part because this seems wasteful as not much is displayed on each part

Related

how to program a button from main view controller to open another view controller - firestore records

I'm building my first app using Swift and have done a few modules from the CodeWithChris website. I'm just doing this as a hobby.
The main ViewController has a tableView with a list of records that are retrieved from FireStore. If you click one of the records, it will open another ViewController through a prepare for segue function and brings up a detail screen with various fields from that record for editing.
So back to the main view: In the tableView prototype cell, I have a horizontal stack view in which has two of the fields from the record and then I've added a button which I'd like to open a separate View different from the first I've described above but similar - so that it will bring up a detailed record view but with an entirely different set of fields that the other.
The concept is I'm tracking animal health histories so it shows a list of the animal names and types and when you click on the name or type, it goes to that first view with details about that animal. Then from that main tableview, I have a button that I want to bring up the vaccination history. I tried to do a prepare for segue by dragging the button to a new view controller but that didn't seem to work.
I'm just looking for the general idea of what I have to do and can probably use that feedback to maybe figure it out.
I figured it out. The segue identifier lets me set up some conditions within the prepare for segue function to active my goal!

Change view controller cause reset changes

I have two view controller. On the Second controller I have a button which cause a UILabel to change from "larry" to the string "happy". If I press back, which take me to first view controller, and then forward, which take me to the second view controller (from the first view controller). I found that the UILabel change back to default, "larry". How do I make it so that when I change view controller, the changes and data doesn't get clear away.
Recap: How to make it so that every change in a view controller doesn't change back to default after changing to another view controller.
This doesn't only happen with UILable, it also happen with
self.loadPage.hidden = YES;
I also found out what I made to turn 'hidden' also appear back after I pressed back and forward.
Thank
The problem that you are facing is due to the fact that when you press the button a new instance of the second viewcontroller is created and that old one is destroyed.
If you want to persist the data while pressing the buttons, you need some way to store the information somewhere
there are some methods to got about it
You can use instance variables in both the classes to store the data temporarily and display them. You need to know the concept of protocols and delegates for this as well.
You can store the information in NSUserDefaults for some moment so that you can access it later on.
The issue is each time when you goes from first view controller to second, you are initialising a new instance of second view controller which brings back every values you changed to default values.
You need to save the values elsewhere and assign those values whenever control is switched from one view controller to another

Maintaining a view after navigation to different views in swift

Can anyone tell me how to maintain the state of a view even while navigating through different views. I have a main view controller that has some button interaction and label changes with those interactions and I want to maintain those states while the user navigates to different pages.
It is not a good idea to "save" ui object, it is instead better to save the data you want to use somewhere (like a file) and get it when needed

How to pass a button between two views?

Is it possible to pass a button between two views?
I mean, when the button is clicked it has to call another view and stay while the view behind it pushesToSegue away.
Are you trying to reuse the same button in multiple view controllers? That is not really how things are done normally in iOS.
I suppose you want to pass some data attached to the Button (i.e. its label or tag) to the next view controller. In that case, you would want to pass the data as properties (e.g. NSString, ...etc.). In your next view controller, create the same button in the same position in the storyboard. Then you can set its label from the data you have passed in.
Now if you really want to pass a UIButton to the next view controller, you can still pass it as a property. However, when someone taps the button, the action method that is called is still the one in your first view controller. You could remap it to another action method in the next view controller, but going down this path is against the general rule of thumb.

Refresh split view

I have a universal application with a list of items (loaded from a backend) and item details (loaded from a backend as well). Each view controller listens to the UIApplicationDidBecomeActiveNotification, so that the view is refreshed when the user (re-)opens the app. That works so far.
Now my problem. On iPad, I have a split view. So, when the user (re-)opens the app in landscape, both views are reloaded. If there is no connection to the backend, the user gets two alerts with Retry/Cancel options, one above the other. That is not what I want... I have one default item which does not require connection to the backend and I want to set it to be selected and to display its details in the detail view. Always when the originally selected item is missing in the master view.
What I have done so far... In my master view controller, I check whether the selected item is available after the refresh and if not, I update the selection and the detail view. This should solve the problem when the requests from the left and from the right pane are processed in the correct order. However, currently both view controllers get the UIApplicationDidBecomeActiveNotification and make asynchronous requests to the backend.
Has someone experience in refreshing split views? What would be the right way to solve my problem? I really don't want to introduce additional notifications/complexity. I hope, there is some standard way to reload the data.
Well, I found a solution.
I create for each view controller a property alertView and store there the alert view that is displayed. In viewWillDisappear, I dismiss this alert view. So, when the details for my default item are displayed to the right, the alert view of the "old" controller is dismissed and I have only one alert view.
It is for sure not the perfect solution and I would be happy if someone can give me a better one. But for now, that works fine.

Resources