I have four ViewController, I don't use an UITabbedbar because It's more difficult to customize. I use modal segue but I think the memory consumption is excessive. this is a screen shot of my first and second VC. What I have to use to change View correctly?
That's the code I use :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "second") {
let secondVC = segue.destinationViewController as SecondViewController;
}
}
From your Storyboard diagram, it is clear that you have created a segue from each button in your "tab bar" to another view controller. Except for the unwind segue, segues always create a new instance of the view controller they are switching to. So if you use your setup to switch from view controller 1 to view controller 2 and then back to view controller 1, you won't be returning to the view controller you came from but instead you will be creating an entirely new view controller 1.
This is why your memory consumption is excessive. You keep creating view controllers until your app crashes.
I would recommend you return to using a tab bar controller. They were designed to allocate the view controllers once up front and then just switch between them. Also, they have a standard look for a reason, it helps the user of your app know immediately how to interact with them.
To pass data between tabs, you won't use segues because there is no segue happening when you switch tabs. There are many ways you can do this, but they all boil down to having model data stored where all of the tabs can access it. This can be done with CoreData in a larger app. For a simple app, you can do the following:
Create a custom subclass of
UITabBarController
. Let's call itCustomTabBarController
. Have that class create and hold the model data that will be accessed by each of your tabs.CustomTabBarController.swift:
In your Storyboard, in the Identity Inspector, change the class of
UITabBarController
toCustomTabBarController
.In
viewWillAppear
in each of your tabs, get a reference to the model data and then you can use it.FirstViewController.swift:
SecondViewController.swift: