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.
Related
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.
This is probably a very simple question but I can't find the answer to it.
I am working on a new project that uses Storyboards for the first time.
I have a number of view controllers that connect the way I want them to.
Each view controller has an info button.
I have one view controller (AboutViewController) that I want to use to display the info for all the view controllers. I am currently calling this via a popover segue from each screen. So I have one destination view controller (AVC) that I am calling from a number of VCs- VC1toAVC, VC2toAVC, VC3toAVC etc. I want two textfields in AVC to change, depending on which VC called it.
So here's the problem- how can I tell which view controller called the popup? It's basically the view that's below the popover. Currently I'm storing it as a variable but that's not ideal. I'm guessing it has something to do with the segue identifiers?
Any and all help much appreciated!
One approach to this is adding a property to your pop up view controller and then define the
prepareForSegue:sender:
method so you set your destination view controller's property to the sender of the segue.
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)
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.
Is it possible to create a storyboard segue from a view controller to itself? I have a bunch of Entities that have Related Entities. I'd like to be able to display a Related Entity using the same view controller that's displaying the Entity. But I can't seem to create a segue that will display a new instance of the origin view controller.
Is it just not allowed? Thanks!
Well here's a solution that isn't quite the same but gets me what I want. I found it as an answer to this question.
The reason I thought I had to use a segue rather than the good old programmatic push of a view controller onto the navigation controller's stack is that I had set up the view controller's IBOutlets in the storyboard. I didn't realize that you could create a copy of the view controller as laid out in the storyboard without using a storyboard segue. You can! To see how to do it, check out that other question and up vote the answerer!
You can ctrl-click-drag (or right-click-drag) from an element (UIButton, etc.) to the containing view controller.
(Did you try this? I'm doing it right now; I have one stock UIViewController that just keeps adding itself indefinitely to the containing UINavigationController stack via a normal push segue.)
Yeah, it's annoying I can't do a 'manual' segue to itself.
What I did was added a UIButton to my view and gave it an action of push to the same view controller, and then made this button hidden. Then I can name the segue and reference it in the code.
Hacky, but works.