go to previous view controller - ios

I've two view controller.
In the first, i write my room's name (for example bedroom) and push the button.
In second view, i see the name of my room, and insert other details, like number of bulbs, name of bulbs and when i've finished, I press on a button that keeps me coming back to previous controller.
I'm using uinavigation bar for this two steps, but i have a question.
If I press on bedroom's button, i don't see the details that i've wrote before.
How i can see that?

As you have discovered, when you go back from your second view controller to the first, the second is deallocated and its contents are lost. When you go forward again, a completely new instance of your second view controller is created.
To overcome this, you need to pass the current data from the first to the second, and then pass back any changes when the user goes back to first (before the second view controller is deallocated). There is a very good explanation of this, with various options for achieving it, in this question.

just you add on button click this method .
[self.navigationController popToRootViewControllerAnimated:YES];

Related

Data passed through segue lost after hitting back button

I have two view controllers A and B and a segue from A to B (type: show) through a next button and a segue from B to A (type: show) through back button. I am passing some data through segue A to B. Data is displayed correctly if I just tap next from controller A and go to controller B.
However, If I tap next and go to controller B and hit back and come back to A and then hit next again, it doesn't work. Any ideas whats going on here? Sorry for the confusing language.
Sounds like your "B to A" is wrong, you're using a navigation stack so you should only push (show) when you are moving forwards. When going backwards you should pop off the top (dismiss)
If you keep pushing, the app will continue to just put more and more ViewControllers in memory, which is bad.
I suspect whats happening here is View A initially has the data and passes it to View B. Then when you press your back button it creates a new copy of View A which doesn't have the data and shows that instead of the original View A.
In Short: (A to B :type - show) (B to A :dismiss the current ViewController)
I agree with user2877496 in that you should not use a regular segue to go back from B to A as you will be adding "A" onto the stack each time.
I just wished to add that one method to dismiss B and go back to A is by using an Unwind Segue.
The Apple documentation covers this quite well
https://developer.apple.com/library/content/technotes/tn2298/_index.html
Did you resolve this? My guess is you put stuff to execute in viewDidLoad (which only runs once at the start of your app) and it needs to be in viewWillAppear so it is called each time you return to your main view controller. Please provide your code for those methods if you require further help.

ios swift how to set a save button in the navigation bar

I have a MainViewController which is a table view controller, with custom cells where the user can select some data. There is a button on this MainViewController that goes to PreferencesViewController, which has two buttons on the navigation bar, one for back (it is wokring now good), and the other one for save (here is my problem)
This is a screenshot of the navitation bar in the PreferencesViewController:
I drag a segue from the save bar button back to the MainViewController.
my problem
when I hit save, the MainViewController appears, but without saving the values the the user has already selected. sounds like a new instance of this MainViewContoller is being created.
What am i missing here? what other approach i should have already used please?
You need to connect your save button to an IBAction, not a segue.
In that IBAction, do whatever you need to do to save your data, and then do a dismissViewController:animated: call yourself in code.
If you wanted to use a segue you wouldn't be able to (easily) save your data, and yes, it creates a new instance.
An alternative would be to connect your save button to an unwind segue, rolls back, or unwinds, one or more segues to get back to a previous view controller. To do that you'd have to put your save logic in prepareForSegue (with code to test which segue is being invoked and only save in response to the save segue.)
i found the solution myself using:
1- custom delegate:
2- self.navigationController?.popViewControllerAnimated(true)

My Navigation Controller Count Keeps Growing

I'm new to iOS dev and am not entirely sure on Storyboards/Segues/Pushing and Popping.
So in my app I have a navigation controller with 5 view controllers leading from one to another.
When it reaches the last view controller i have a segue to the first and I have a few tasks listed in the prepareForSegue method.
Out of curiosity I decided to check what happens to the [self.navigationController.viewControllers count]. I found that it keeps growing and growing which doesn't 'feel' correct.
Am i handling going back to the first screen correctly? The prepareForSegue method is useful as it allows me to send some data back to the first segue. Is it possible to maybe say when you go back clear all views on that navigation controller?
Thanks
You can use an unwind segue. Here's a good tutorial:
pragmaticstudio.com/blog/2013/2/5/unwind-segues
Make sure to create the unwind action method before you wire it up in the storyboard otherwise it won't show up when you drag to 'Exit'. That was the most confusing part for me when I first set one up. The tutorial does it in the correct order so if you follow it you should be fine.
Also, here's a sample I put together showing how to transfer data back in an unwind segue. It uses a modally presented view controller but the technique is the same:
github.com/soleares/AddToTableView
No, you should never go backwards with a segue (other than an unwind). Segues ALWAYS instantiate new controllers, so you're not actually going back to the first controller, you're just creating a new instance, which gets added to the stack. So either go back with an unwind segue or use popToViewController:animated:. Using an unwind segue will allow you to use prepareForSegue, and it does cause all the controllers in between to be deallocated (if you have no other strong pointers to them).

xcode stay on second view controller when application open after long time

I have first view controller and second view controller. I will go from first view controller to second view controller. After that, I will go to other application or browse for some time. Then, when I come back, it always go back to first view controller as the new application open. How shall I just stay in particular view after I come back ?
Read about State Preservation and Restoration , it's very useful in your case
Do you mean your app again start with the first view controller, after coming into active state?
I think app start from where it left.
Either you're doing something in the code to load the first view controller again, or
you can store the state and on coming into foreground can reset the previous state.

How to catch the moment, when the UIViewController is pushed to the front because back button was pressed?

Well, the question is in the tittle:
How to catch the moment, when the UIViewController is pushed to the front because back button was pressed?
I need it to reload data on the list when it's UIViewController is on the front again. The problem is I don't want to do that every time the view appears, for example when the view is first shown.
The easy way is to set a flag on your first controller, when you push into another view set it to YES and on your viewWillAppear: check if you need to do some reloading.
The other option, assuming your second view will configure some things for the view A, is to create a delegate method that let's the view A know it needs reloading.
The third option is to use a custom back button (rounded, not with the back button shape), capture the event and do the popViewController manually after you do whatever you want to do.
then you make a new method and call it before the push (pop) on front the view controller and this method help you to reload the list on itself class also :-) i hope you understand what i want to say then when you want to reload the data you simply call that function and list is reload .

Resources