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 的转场。除了 unwind segue 之外,segues 总是创建它们要切换到的 View Controller 的新实例。因此,如果您使用您的设置从 View Controller 1 切换到 View Controller 2,然后返回到 View Controller 1,您将不会返回到您来自的 View Controller ,而是您将创建一个全新的 View Controller 1。

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

我建议您重新使用标签栏 Controller 。它们旨在预先分配一次 View Controller ,然后在它们之间切换。此外,它们具有标准外观是有原因的,它可以帮助您的应用用户立即了解如何与它们互动。


要在选项卡之间传递数据,您不会使用 segues,因为切换选项卡时不会发生 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.
        }
    }
    
  2. 在您的 Storyboard 中,在身份检查器中,将 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/27618737/

相关文章:

ios - UISearchBar 未更新

objective-c - 为什么 MPMoviePlayerController 没有暂停?

ios - 无法隐藏以编程方式创建的 UIButton

swift - UIImages 后面的 SpriteNode

swift - 为什么这个 guard 声明会给我一个错误?

ios - UIViewController 有多邪恶?

ios - 为什么我不能在 Swift 中调用 UIViewController 上默认的 super.init() ?

ios - 如何使 UICollectionView 单元格可移动?

objective-c - Instagram 如何将 UIKeyboard returnKey 自定义为 '@' 和 '#"?

io - swift 是否有写入字节流的协议(protocol)?