Solving errors in view transitions (storyboards) - ios

I'm developing an application that works with 2 different views. Today I noticed that every time I step from view 1 to view 2 the app creates a new view.
And when I return to the previous view it simply creates a new instance of view1, ie it creates a new view in memory. (and keep the previous)
I would like to use the same view without creating a new instance (a new view in memory) each time the user passes a view to another.
I would like transit between just the 2 views that I created.
Note: I'm using the iOS 6 with Storyboard and ARC. And the transition is made by a tap gesture recognizer. Once I tap the view it goes to the next view using a modal transition.

When you go from view controller 1 to view controller 2 as a modal (presented) view controller, you use a modal segue. This will indeed create a new instance of view controller 2. When you go back, in iOS 6, you must use an unwind segue to the first view controller. This will cause the view controller 2 to go out of existence, and it will use the existing view controller 1, not create a new view controller 1.
Here's my explanation of unwind segues.
http://www.apeth.com/iOSBook/ch19.html#_unwind_segues
(Either that, or don't use a segue at all; just call dismissViewController yourself, as we did in iOS 5 before unwind segues existed. Or don't use a storyboard in the first place; they really aren't necessary. Here's my explanation of presented ["modal"] view controllers: http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller)

Related

custom container view controller vs normal segue and rewind segues

My intent was to create a master scene and view controller that was in control most of the time, but when logic demanded it would segue to one of several other scenes. Each of these "other" scenes would return to the master view controller using rewind segues. Which other scene was chosen was determined by logic in the master view controller. This worked fine until I found a need to transition from one of the "other" scenes to a different "other" scene without presenting anything from the master scene. I want to put a performSegueWithIdentifier in the master view controller before anything is presented.
I have tried putting the performSegue.. in the method that catches the rewind segue, in the viewWillAppear method, in a block kicked off async from the rewind method, but in all cases I see the performSegueWithIdenitifer method execute, then the appropriate prepareForSegue but then control returns to the master view controller and the scene I get in the device is from the master view controller.
I tried to look at other methods and it would seem that building a custom container view controller is one way of doing this, but it does not seem right since all the "other" scenes I want to segue to are push segues and take over the entire screen.
Is there some reason that a view controller that has received control from a rewind segue cannot then kickoff a segue without ever appearing itself? Is the Custom container view controller the correct way to do this? And if the latter, do I let the storyboard set up the embed segues to all the other view controllers, or do I leave them unconnected in the storyboard and load them in code?
Thanks in advance, ##warning - novice here - this is my first real project##

iOS 7 - Maintain view controller instance in Storyboard segue

I have an interactive custom view controller transition based on a storyboard segue (push).
The target view controller takes some time to be loaded as it contains a table with a lot of data; moreover when I leave this vc and come back, I need the table to maintain its content offset and not to start each time from the first row.
In order to achieve these two points I need the target vc to be a kind of singleton, and not to be deallocated/reallocated every time.
Any suggestion?
Thanks,
DAN
Don't use a segue -- they always instantiate new view controllers. Create a property for the destination view controller in the controller that initiates the transition, and only instantiate it the first time you go to it. Push the new controller in code.

How to save values in view controllers during app running? ios7

I got app with 2 view controllers, I'm typing values via NSStrings in Label and TextField in my first view controller and when I by pushing my navigation button go to my second view controller.
When I return to my first view controller, I got entered early values. But when next after that I go to my second view controller - values entered via NSStrings in Label and TextField disappear. How to fix this that the values ​​saved?
I tried to use strong and copy properties but that not helps me.
UPDATE
I use segue and storyboards, segue with modal type, I use 2 navigation controllers: for first view controller and for second. I got code only for modal type. First view controller is root for navigation
UPDATE
I use 2 navigation controllers because I need modal segue from first VC to second and from second to first, when I use one navigation controllers that is not works like I need
Post some code otherwise we are just guessing. One guess is that you are using segues. Segues always create a new instance of the viewController. If you don't hold on to the old viewController somehow, it will get destroyed and any values it contained are also gone.
Just a guess.

Using Storyboards Segues

I have a question about how I use segues with Storyboard in Xcode.
I have an application with 3 views, and I would load them with animation modal "Cross Dissolve".
Every time I load a new view without dismiss the current one, it still occupying memory?
I'm realizing that after changing multiple views my APP becomes slow.
If yes, how is the right way to change views in sequence?
When you go back from 3 to 1 you should use an unwind segue. That will cause 3 and 2 to be deallocated (if you're not keeping a strong pointer to them), and you will actually go back to the same instance of 1 (rather than creating a new one). In general, you should not go backwards using a segue other than an unwind segue.
Rather than specifying the "back to 1" segue in the storyboard, you may want to instantiate it in code using the method -initWithIdentifier:source:destination:. That'll permit you to specify the destination as your first view controller instead of creating a new view controller to transition to.
In fact, you should probably specify all the segues between these view controllers programmatically if you don't want to instantiate new copies with each switch.

Dismissing a view makes losing it all values... viewWillAppear as a cure?

I have a basic modal view system.
My app loads the UI base in which there are 2 buttons presenting 2 other views.
In those views, a dismiss button.
Everything works fine.
BUT, in one of the 2 modal views, I have a bunch of UISlider & UISwitch.
I want them to retain their values but the dismiss loses them: as soon as I trigger the button to show the view containing the UI elements, this view is shown with all values for all elements as I put initially in the xib.
should I store all values in variables, then in viewWillAppear I could "recall" them ?
would you advice me another strategy ?
Yes, your proposed approach is exactly the right sort of thing. But be careful; viewWillAppear can be called for many reasons; make sure you're only doing this when the view controller is coming into existence and showing the view for the first time.
NSUserDefaults can be an excellent place to store globally needed info like this. In viewWillDisappear, store the desired state info (values of the sliders and switches) in defaults. Then retrieve them the next time the view is about to appear.
When you create the modal view you are creating a new instance of the modalViewController an the modalView. This new instance knows nothing about any other instance. There are a few ways you can retain the information from previous iterations of these modal view controllers.
How I would do it:
Set up place holders in your main view and pass the values that the user selects back to the main view via a protocol and delegate setup. Then when you segue to the modal view you can load those variables in before displaying the modal view.
So let's say you have a dictionary with all of the values: {slider = YES, someValue=10,...} Create that dictionary in the main view controller, the first one that opens, and place some default values in it.
In your modal view controllers create the same dictionary as a property.
Create a protocol in your modal view controller with a method that is something like
- (void) doneEditing:(NSDictionary *)values
Set up your first view as the delegate for the modal view controller and in the implementation of doneEditing copy the values to the dictionary that is present in the first view before popping the modal view.
When the first view is ready to present the modal view again, copy the values to the dictionary property of the modal view before presenting it.
I hope this gets you headed in the right direction. It's important to remember that each time you segue or create and present a modal view you are creating a brand new instance of that view, it knows nothing about the previous instance at all unless you tell it something about it.

Resources