ios - 改变 Swift 中的 VC 问题。如何在标签栏 Controller 的 View 之间传递数据?

标签 ios swift uiviewcontroller uitabbarcontroller modalviewcontroller

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

enter image description here


这是我使用的代码:

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


    }
}

最佳答案

从您的 Storyboard图中,很明显您已经创建了从“选项卡栏”中的每个按钮到另一个 View Controller 的segue。除了展开segue之外,segues 总是创建它们要切换到的 View Controller 的新实例。因此,如果您使用设置从 View Controller 1 切换到 View Controller 2,然后返回 View Controller 1,您将不会返回到原来的 View Controller ,而是将创建一个全新的 View Controller 1。

这就是你的内存消耗过多的原因。您不断创建 View Controller ,直到您的应用程序崩溃。

我建议您重新使用标签栏 Controller 。它们被设计为预先分配 View Controller ,然后在它们之间切换。此外,它们具有标准外观是有原因的,它可以帮助您的应用程序的用户立即知道如何与它们交互。


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

    enter image description here

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

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

关于ios - 改变 Swift 中的 VC 问题。如何在标签栏 Controller 的 View 之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55146464/

相关文章:

ios - Swift 良好编码实践 : If statement with optional type Bool

ios - UICollectionViewCell : can I know exactly when the cell appears and disappears?

ios - Swift 中的 sizeWIthAttributes 出奇地慢

objective-c - 将 .xib 文件添加到 UIViewController 子类?

ios - 连接在一起 : AppDelegate, ViewController 和 XIB

ios - 横向 xib 显示为纵向

ios - modalPresentationStyle 在 iOS5 的模态视图中不起作用

ios - swift : Error when the same button is reused. 错误是 "<UIButton: ; frame = ; opaque = NO; autoresize = RM+BM; layer"

ios - 展开/最小化 UITableView 自定义单元格

uiviewcontroller - 在Segue中如何从 View Controller 引用tableView