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.
Related
I have two view controllers, one that shows some content in a list and one that renders a map. I give users the option to switch between the two by tapping a button. I'm currently using a segue for the button to switch between the two views, but because of the nature of segues I end up re-initializing the view controller each time, generating unnecessary network traffic.
I'm looking for a way to transition between these two views in such a way that the controllers are only initialized once, the first time they're invoked. Subsequent invocations of these controllers would just use whatever instance was already loaded in memory.
Is this achievable? If so, what do I use? pushViewController also ends up calling viewDidLoad on the controller being pushed onto the stack
So I am trying to programmatically change views when some condition is triggered.
I have created a simplistic project, whereby I want to change to View 2 from View 1, once View 1 has loaded. In reality, there is some logical operation that checks if some variable is true, then it invokes the performSegueWithIdentifier function, however to keep this as simple as possible I have removed that code.
The steps I have performed are, firstly to create two views:
I then click View 1, and inspect its properties and then created a manually triggered segue to View 2
I selected the 'Show' option, so now my Views are connected by a segue
Then under attribute inspector, I give the storyboard segue and identifier of "GoToView2".
From there I go to the ViewController.swift file, and in the viewDidLoad function. I insert performSegueWithIdentifier code:
self.performSegueWithIdentifier("GoToView2", sender: self)
However, when I then run the code. View 2 does not load, and only View 1 appears in the simulator.
Any help and explanation would be greatly appreciated.
I have had the same problem with attempting to perform a segue inside the viewDidLoad function, and the error that comes up seems to be that the view you are attempting to load with the segue is not in the window hierarchy.
Changing the call to viewDidAppear seemed to fix the issue as mentioned in this other post: whose view is not in the window hierarchy (note the language is objective-c not swift, but it can be translated).
It seems you have to be careful at what point in the view lifecycle you want to change it to another view controller - at viewDidLoad all the views in the storyboard are not available.
BTW, without having a conditional in there, the segue will be fired (and load the view controller) every time the view is loaded!
I am working on a project that does exactly what you want to do. Perform segue with identifier is the right thing to do. My project also does the check in viewDidLoad.
All your steps look fine, so I believe it's a subtle issue. Check the identity inspector to see if you connected the view controller with view 1
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.
What I need to do is basically build a container(view controller) that can change its child view controller dynamically and also set it's initial view controller dynamically.
I never used the Embed segue before so I thought I'll give it a shot.
However, using it seems to allow me to change the child view controller dynamically using a custom segue between the children view controllers but the initial view controllers seem to be fixed to the one I dragged the segue to in the StoryBoard(The custom segue here would be something alone these lines).
I know I can achieve what i'm looking for by creating x custom segue (where x is the number of children VCs I need) from the container view controller directly to the children and just calling these segues in code based on my needs.
But if that's the only way, what's the reason for using the "Embed" segue, is it only for really simple scenario's ?
An embed segue is not just for really simple scenarii. It can get pretty complicated. A major purpose is to cleanly separate code related to different concerns, that may still coexist on the same screen, into different view controllers. For instance, you could have an authentication controller and a preferences controller, both embedded into a single profile controller.
I want to swap out one view controller for another in an existing XCode iOS storyboard, but I'd rather not recreate the segues going to it. Is it possible to move or adjust the destination of an existing segue?
I believe you do need to create the new segue(s), but that's as easy as control-dragging between the source of the segue and your new scene. And if your code is doing anything that requires the storyboard id (such as prepareForSegue or performSegue), then just apply the same storyboard id for your new segue and you don't need to change your code. If you're using a custom segue, you can just use the same segue class, so you don't need to change the code just because the destination changed. And if the source of your segue is a button or something like that, when you make the segue to the new scene, your old segue is automatically removed, so that simplifies the process, too.
In short, you probably do need to recreate the segues, but you probably don't need to change your code too much to support that.