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

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

问题描述

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

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;


    }
}

推荐答案

从您的 Storyboard 图中,很明显您已经创建了从标签栏"中的每个按钮到另一个视图控制器的转场.除了unwind 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.

要在选项卡之间传递数据,您不会使用转接,因为切换选项卡时不会发生转接.有很多方法可以做到这一点,但它们都归结为将模型数据存储在所有选项卡都可以访问的地方.这可以在更大的应用程序中使用 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.
    }
}

  • 在您的 Storyboard 中,在 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天全站免登陆