在Swift中改变VC问题.如何在标签栏控制器中的视图之间传递数据?

4 uitabbarcontroller uiviewcontroller modalviewcontroller ios swift

我有四个ViewController,我没有使用UITabbedbar,因为它更难定制.我使用模态segue但我认为内存消耗过多.这是我的第一个和第二个VC的屏幕截图.我必须使用什么来正确更改View?

在此输入图像描述


这是我使用的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "second") {
        let secondVC = segue.destinationViewController as SecondViewController;


    }
}
Run Code Online (Sandbox Code Playgroud)

vac*_*ama 13

从故事板图中可以清楚地看到,您已经从"标签栏"中的每个按钮创建了一个segue到另一个视图控制器.除了展开segue之外,segues总是创建一个他们切换到的视图控制器的新实例.因此,如果您使用您的设置从视图控制器1切换到视图控制器2然后再返回到视图控制器1,您将不会返回到您来自的视图控制器,而是您将创建一个全新的视图控制器1.

这就是你的内存消耗过多的原因.在应用程序崩溃之前,您将继续创建视图控制器.

我建议你回到使用标签栏控制器.它们被设计为预先分配视图控制器,然后在它们之间切换.此外,他们有一个标准的查找原因,它可以帮助您的应用程序的用户立即知道如何与他们进行交互.


要在选项卡之间传递数据,您将不会使用segue,因为切换选项卡时不会发生segue.有很多方法可以做到这一点,但它们都归结为模型数据存储在所有选项卡都可以访问它的位置.这可以通过更大的应用程序中的CoreData来完成.对于简单的应用程序,您可以执行以下操作:

  1. 创建一个自定义子类UITabBarController.我们称之为CustomTabBarController.让该类创建并保存每个选项卡将访问的模型数据.

    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.
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在Storyboard中,在Identity Inspector中,将类更改UITabBarControllerCustomTabBarController.

    在此输入图像描述

  3. viewWillAppear每个选项卡中,获取对模型数据的引用,然后您可以使用它.

    FirstViewController.swift:

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

    SecondViewController.swift:

    import UIKit
    
    class SecondViewController: UIViewController {
        @IBOutlet weak var nameLabel: UILabel!
        @IBOutlet weak var ageLabel: UILabel!
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            // 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)"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)