我是swift的新手,我有一个带有5个不同视图控制器和5个不同视图controller.swift文件的标签页应用程序。每个视图控制器都有类似的语法,但有一些名称和标签不同。每个控制器都有一个功能,它将重新启动我的数据值,它们都可以在每页上运行。但我想要做的是,只需从一个视图控制器按一个按钮,即可从每个视图控制器访问清除功能。我的格式/逻辑可能完全关闭。我自学,所以任何帮助表示赞赏
import UIKit
class FirstViewController: UIViewController {
// North wing
var nTotal = 0
var total = 0
//Stepper Values
@IBOutlet weak var tStepper: UIStepper!
@IBOutlet weak var cStepper: UIStepper!
@IBOutlet weak var ssStepper: UIStepper!
@IBOutlet weak var sStepper: UIStepper!
//-Tables
@IBOutlet weak var Tables: UILabel!
@IBAction func TCounter(_ sender: UIStepper) {
Tables.text = String(Int(sender.value))
}
//-Carrels
@IBOutlet weak var Carrels: UILabel!
@IBAction func CCounter(_ sender: UIStepper) {
Carrels.text = String(Int(sender.value))
}
//-Soft Seating
@IBOutlet weak var SoftSeating: UILabel!
@IBAction func SSCounter(_ sender: UIStepper) {
SoftSeating.text = String(Int(sender.value))
}
//-Standing
@IBOutlet weak var Standing: UILabel!
@IBAction func SCounter(_ sender: UIStepper) {
Standing.text = String(Int(sender.value))
}
//-Clear Pop-up
@IBAction func Clear(_ sender: UIButton) {
let alert = UIAlertController(title: "Clear", message: "Clear all or current page?", preferredStyle: .alert)
let clearPage = UIAlertAction(title: "Clear Page", style: .default, handler: {ACTION in
self.nClear()
})
let clearAll = UIAlertAction(title: "Clear All", style: .destructive, handler: {ACTION in
self.MasterClear()
})
let Cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(clearPage)
alert.addAction(clearAll)
alert.addAction(Cancel)
self.present(alert, animated: true, completion: nil)
}
这是我所有视图控制器上的清晰功能
//Clear Function
func nClear() {
self.Tables.text = "0"
self.Carrels.text = "0"
self.SoftSeating.text = "0"
self.Standing.text = "0"
//Stepper Clear
self.tStepper.value = 0
self.cStepper.value = 0
self.ssStepper.value = 0
self.sStepper.value = 0
}
这是我希望能够访问每个Controller上所有清晰功能的功能
//Clear all Function
func MasterClear(){
self.nClear()
SecondViewController().iLClear()
}
当我运行并访问主清除功能时,我的代码中断和应用程序崩溃
答案 0 :(得分:0)
您真正想要做的是为每个视图制作一个您想要在不同视图之间共享的数据模型。我将用一个样本做一个样本。为您的数据模型创建一个swift文件,如下所示:
class TestModel {
var Tables: String = "0"
var Carrels: String = "0"
var SoftSeating: String = "0"
var Standing: String = "0"
//Stepper Clear
var tStepper: Int = 0
var cStepper: Int = 0
var ssStepper: Int = 0
func modelReset() {
Tables = "0"
Carrels = "0"
SoftSeating = "0"
Standing = "0"
tStepper = 0
cStepper = 0
ssStepper = 0
}
}
接下来在第一个视图控制器中添加一个变量,该变量是模型的实例
class FirstViewController: UIViewController {
var modelController: TestModel!
并在viewDidLoad函数中初始化变量
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
modelController = TestModel()
}
创建清晰的功能。我打电话给我重置:
@IBAction func reset(_ sender: Any) {
//Do for each ViewConrtoller
modelController.modelReset()
}
现在覆盖prepare函数以将数据发送到我们将要进行的视图控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondViewController = segue.destination as? SecondViewController {
secondViewController.modelController = modelController
}
}
现在在你的第二个视图控制器中创建一个模型的变量,但在这个实例中我们不会在viewDidLoad函数中初始化它。
class SecondViewController: UIViewController {
var modelController: TestModel!
当我们从准备功能中的第一个视图控制器传递它时,我们不会初始化它。
第二个视图控制器中的viewWillApear函数使用模型中的数据更新视图
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.Tables.text = modelController.Tables
self.Carrels.text = modelController.Carrels
self.SoftSeating.text = modelController.SoftSeating
self.Standing.text = modelController.Standing
//Stepper Clear
self.tStepper.value = modelController.tStepper
self.cStepper.value = modelController.cStepper
self.ssStepper.value = modelConroller.ssStepper
}
最后一步不要忘记实现prepare函数,以便在您更新模型时将模型传递回第一个视图控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondViewController = segue.destination as? SecondViewController {
secondViewController.modelController = modelController
}
}