Ios 在Swift中更改VC问题。如何在选项卡栏控制器中的视图之间传递数据?
我有四个ViewController,我不使用UITableBar,因为它更难自定义。 我使用模态segue,但我认为内存消耗过多。 这是我的第一个和第二个VC的屏幕截图。 我必须使用什么来正确更改视图Ios 在Swift中更改VC问题。如何在选项卡栏控制器中的视图之间传递数据?,ios,swift,uiviewcontroller,uitabbarcontroller,modalviewcontroller,Ios,Swift,Uiviewcontroller,Uitabbarcontroller,Modalviewcontroller,我有四个ViewController,我不使用UITableBar,因为它更难自定义。 我使用模态segue,但我认为内存消耗过多。 这是我的第一个和第二个VC的屏幕截图。 我必须使用什么来正确更改视图 这就是我使用的代码: override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "second") { let secon
这就是我使用的代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "second") {
let secondVC = segue.destinationViewController as SecondViewController;
}
}
从情节提要图中可以清楚地看出,您已经创建了一个从“选项卡栏”中的每个按钮到另一个视图控制器的序列。除“展开”序列外,序列始终会创建要切换到的视图控制器的新实例。因此,如果使用设置从“视图控制器1”切换到“视图控制器2”,然后再切换回“视图控制器1”,则不会返回到来自的视图控制器,而是创建一个全新的视图控制器1 这就是内存消耗过多的原因。一直创建视图控制器,直到应用程序崩溃 我建议您重新使用选项卡栏控制器。它们被设计为预先分配视图控制器一次,然后在它们之间切换。此外,它们的标准外观也是有原因的,它可以帮助应用程序的用户立即知道如何与它们交互
要在选项卡之间传递数据,您不会使用分段,因为切换选项卡时不会发生分段。有很多方法可以做到这一点,但它们都归结为将模型数据存储在所有选项卡都可以访问的位置。这可以在更大的应用程序中使用CoreData完成。对于简单的应用程序,您可以执行以下操作:
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.
}
}
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)))]
}
}
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)"
}
}
UITabBarController
的类更改为CustomTabBarController
视图中,将在每个选项卡中显示,获取模型数据的引用,然后您可以使用它
FirstViewController.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.
}
}
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)))]
}
}
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)"
}
}
SecondViewController.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.
}
}
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)))]
}
}
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)"
}
}
你想用这些代码做什么?(到目前为止,它实际上没有做任何事情…)我已经调用了segue“second”,您在代码中没有调用segue。。。您只是对即将发生的序列做出反应,将secondVC设置为一个局部常数,而不使用它。如果你删除了整个方法,你会看到segue仍然在执行。我认为你的答案基本上是正确的,但是OP更新了她的答案,以澄清她没有使用标签栏。也许你也应该编辑你的答案来解释你的推理是如何仍然适用的。我如何使用tab bar在视图之间传递数据?@Mala,这行只是从名称列表中选择一个随机名称并将其分配给model.name。这只是一个演示,让示例做一些有趣的事情。我如何在其他地方使用该模型,而不是
视图将出现?我的意思是,如何使它在同一个viewController中可用?因此,它可以在不同的函数中使用,viewController中的任何方法都应该能够以ViewWillDisplay相同的方式获取指向模型的指针。该方法很方便,因为它在每次选择选项卡时都会运行。