在Swift中改变VC问题。如何在标签栏控制器中的视图之间传递数据? [英] Changing VC issue in Swift. How to pass data between views in tab bar controller?

查看:100
本文介绍了在Swift中改变VC问题。如何在标签栏控制器中的视图之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我使用的代码:

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


    }
}


推荐答案

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

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.

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

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:


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

  1. Create a custom subclass of UITabBarController. Let's call it CustomTabBarController. Have that class create and hold the model data that will be accessed by each of your tabs.

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.
    }
}


  • 在故事板中,在Identity Inspector中,将 UITabBarController 的类更改为 CustomTabBarController

    viewWillAppear 在每个标签中,获取对模型数据的引用,然后就可以使用它。

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

    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)))]
        }
    
    }
    

    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)"
        }
    }
    


  • 这篇关于在Swift中改变VC问题。如何在标签栏控制器中的视图之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆