Swift - How to keep reference to old view controller when instantiating new view controller? - ios

I am trying to programmatically instantiate a new view controller of the same type as my original view controller using this code:
self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier(StoryboardKeys.ViewIdentifier), animated: true)
I would like the user to be able to go back to my original view controller, so I was wondering if there is a way to keep the old view controller in memory? Also if I instantiate a view controller this way, do I get to prepareforsegue (in order to programmatically add a back button)? If not, how could I add a back button when pushing this view controller?
I suspect there is a better way to do this, so if there is, please let me know. Thank you very much for any help!

UINavigationController maintain your view controllers as a stack automatically, so don't worry about your view controller's memory management.
Only if you want a custom back button, otherwise you don't have to add a back button by yourself, everything has been done.

Just use self.navigationController!.popViewControllerAnimated(true) somewhere in your second view controller . Navigation controller contains a stack of views that you can retrieve by using self.navigationController!.viewControllers. You can modify this stack as you like.

Related

Saving the state of the collection view when view controller is changed

I am using a collection view to present the data loaded from firebase. I am not using any sort of embedded navigation controller, just the normal viewcontroller.
Now, whenever I change my view and come back, all data is reloaded and the controller starts from scratch. I actually want it to be on the cell it was left of and how it was left.
I have check plenty of articles and sources on the internet, but most of them are about the embedded navigation controller.
Can anyone guide me on how exactly can I achieve this without using the embedded navigation controller?
Please let me know if anymore information is required. I'll be quite prompt.
I thinks it depends on where you reload your collection view data. You said
Now, whenever I change my view and come back
Did you present your next view controller or push view controller from view controller where your collection view exists? if yes, you may use dismiss or pop view controller to come back.
When you come back to view controller where your collection view exists, viewWillAppear and viewDidAppear method will be called.
Did you fetch data from firebase in one of these method? if yes all the data will be reloaded unless you made boolean flag not to load again.
Hope this help you.

Have 2 container views. Need to change one to a different view controller

So I have a main view controller...
and two view containers. Vcon1 and Vcon2
I have them talking to each other via delegates however I'm not sure how to change the view controller displayed...so a tap on a button in Vcon1 will change the Vcon2 to have a new view controller...
I have a navagation controller in Vcon2 however I'm not sure I want a "stack" of view controllers in there...just want to swap out one for another.
Any suggestions appreciated..
If both of them have been added as subviews on the parentController's view, it's just a matter of calling bringSubviewToFront.
Else you can remove one of the view controllers, and proceed to add the other one. You can read more about view controller containment at objc.io

How to store data after popping a storyboard instantiated view controller to then repush it to the navigation controller stack

I have my UIViewControllers mostly setup using a Storyboard file. In some cases I use the usual pattern of a UINavigationController in combination with UITableViewController to let the user dig into configuration details of the application. Each table view cell causes another view controller to be pushed onto the navigation stack (when the cell is selected) and users can go back to the overview table view controller using the navigation controller's back button.
I have the overview UITableViewController and the detail UIViewControllers set up in the Storyboard, including the connections between them using segues.
Most of it works as expected, when I select a table view cell in the overview the correct detail view controller is pushed onto the navigation stack. But if I then change some views in the detail view controller and go back (possibly because I want to look something up)(thus the detail view controller is popped) and then press the same cell again, the views are reset to their original state and the data I put in is lost.
Of course I could save that data in the overview controller when the detail controller is popped and restore it when it is pushed again, but I wonder whether that is the best way.
In the past, I would have constructed the detail view controller myself and stored it in an instance variable. Then, if I had to push it again, I would push the exact same object (not just an instance of the same class) which would mean that it also saved the state of the view.
But as far as I can tell, using Storyboards, for each segue a new view controller is instantiated, so no data is kept. I also cannot set the destinationViewController property of the segue in prepareforSegue:sender: since it is read-only.
Is there a way to reuse an already created view controller in a segue?
Or is there some other elegant way to store the data to reuse it when the detail controller is pushed again? Ideally something the overview table view controller does not have to know about, since it would have to do store the data for all detail view controller in that case.
I know I could retrieve the view controller from the storyboard, instantiate it myself and push it in tableView:didSelectRowAtIndexPath:, but I like the segues and the overview of the view structure the storyboard provides, so I would like to keep that, if possible.
Update:
I think my description was a bit unclear. I do save the data, already, but currently it is saved in the detail view controller (it's not just "saved" by being in the view, I am not trying to abuse the view to store my data), so going back is an implicit "save my current state in this view" in my case. The problem is, I can't implement that in an easy way without storing the data in the overview controller IN ADDITION to storing it in the detail view controller. And I don't want to store it in the overview controller since it is data that belongs to the detail view controller and should be managed by it.
Of course I could save that data in the overview controller when the detail controller is popped and restore it when it is pushed again, but I wonder whether that is the best way.
I think that is the best way. If the detail view controller is being used to edit the data, generally accepted practice is that the user can only leave it by either saving the data or canceling their changes.

How to find the name of the view controller beneath a popover ios7

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.

How do I create storyboard segue from a view controller to itself?

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.

Resources