Change VC issue in Swift. How to transfer data between views in a tab bar controller?

I have four ViewController, I do not use UITabbedbar, because it is more difficult to configure. I use modal segue, but I think the memory consumption is excessive. This is a screenshot of my first and second VC. What should I use to change the view correctly?

enter image description here


What is the code I'm using:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "second") { let secondVC = segue.destinationViewController as SecondViewController; } } 
+4
source share
1 answer

From the storyboard diagram, it’s clear that you created a session from each button in the β€œtab bar” to another view controller. With the exception of unwinding segue, segues always create a new instance of the view controller to which they switch. Therefore, if you use your setting to switch from view controller 1 to view controller 2, and then back to view controller 1, you will not return to the view controller from which you came, but instead you will create a completely new controller of type 1 .

This is why memory consumption is excessive. You continue to create view controllers until your application runs.

I would recommend that you return to using the tab bar controller. They were designed to distribute view controllers at once, and then simply switch between them. In addition, they have a standard view of the reason; it helps the user of your application to immediately learn how to interact with them.


To transfer data between tabs, you will not use segues, because when you switch tabs, there are no changes. There are many ways to do this, but they all boil down to having model data stored where all the tabs can access it. This can be done using CoreData in a larger application. For a simple application, you can do the following:

  • Create your own subclass of UITabBarController . Let me call it CustomTabBarController . Ask this class to create and hold model data to which all your tabs will be accessible.

    CustomTabBarController.swift:

     import UIKit // This class holds the data for my model. class ModelData { var name = "Fred" var age = 50 } class CustomTabBarController: UITabBarController { // Instantiate the one copy of the model data that will be accessed // by all of the tabs. var model = ModelData() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } } 
  • In your storyboard, in the Identity Inspector, change the UITabBarController class to CustomTabBarController .

    enter image description here

  • In viewWillAppear in each of your tabs you will get a link to the model data, and then you can use it.

    FirstViewController.swift:

     import UIKit class FirstViewController: UIViewController { override func viewWillAppear(animated: Bool) { // Get a reference to the model data from the custom tab bar controller. let model = (self.tabBarController as CustomTabBarController).model // Show the we can access and update the model data from the first tab. // Let just increase the age each time this tab appears and assign // a random name. model.age++ let names = ["Larry", "Curly", "Moe"] model.name = names[Int(arc4random_uniform(UInt32(names.count)))] } } 

    SecondViewController.swift:

     import UIKit class SecondViewController: UIViewController { @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var ageLabel: UILabel! override func viewWillAppear(animated: Bool) { // Get a reference to the model data from the custom tab bar controller. let model = (self.tabBarController as CustomTabBarController).model // This tab will simply access the data and display it when the view // appears. nameLabel.text = model.name ageLabel.text = "\(model.age)" } } 
+12
source

Source: https://habr.com/ru/post/989499/


All Articles