我正在创建一个应用程序,用户可以从3个不同的数组中进行选择,这些数组会更改下一页上某些UI按钮内的文本。我构建了一个粗略演示的应用程序硬编码数组到视图控制器,而不是真正使用模型。现在我已经从视图控制器中获取了所有功能,并将其放在一个单独的类cardDatabase
中。
以下是cardDatabase
类。
class cardDatabase {
public func updateCardSelection(cardSet: Int){
print("updateCardSelection called")
switch cardSet {
case 0:
selectedCardArray = cardSetOne
selectedCardNum = 0
case 1:
selectedCardArray = cardSetTwo
selectedCardNum = 1
case 2:
selectedCardArray = cardSetThree
selectedCardNum = 2
default:
print("Debug Error - Card set not recognised")
}
print(selectedCardNum)
}
public var selectedCardNum: Int!
public var selectedCardArray: Array<String>!
//Create arrays for each card dataset
public let cardSetOne = [
"Word 1",
"Word 2",
"Word 3",
"Word 4",
"Word 5",
"Word 6",
"Word 7",
"Word 8",
"Word 9"
]
public let cardSetTwo = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
]
public let cardSetThree = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i"
]
}
正如您所看到的,有3个阵列填充了不同的单词,这些将在另一个屏幕上显示。我正在努力的是让我的数据库实际存储任何数据。当我移动到显示阵列的屏幕时,我不是直接从数据库中取出,而是通过segue发送信息。
我设想数据库作为应用程序的模型运行,控制器可以从模型中提取信息并在视图中显示它。相反,每次推送新视图时,数据库中的值都设置为nil,使数据库无效!
下面我已经包含了初始视图的代码。这真的让我烦恼,因为我发誓我在以前的应用程序中使用了一个独立的类来存储变量而且我从未遇到过这个问题!
import Foundation
import UIKit
var firstLoad = true
class MainMenuVC: UIViewController {
var cardEngine: cardDatabase!
override func viewDidLoad() {
super.viewDidLoad()
cardEngine = cardDatabase()
if firstLoad == true {
cardEngine.selectedCardArray = cardEngine.cardSetOne
cardEngine.selectedCardNum = 0
firstLoad = false
}
print("selected cardNum = \(cardEngine.selectedCardNum), sender MainMenuVC")
cardSelector.selectedSegmentIndex = cardEngine.selectedCardNum
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Present Card Selection" {
let destinationVC = (segue.destination as! CardSelectionVC)
destinationVC.cardList = cardEngine.selectedCardArray
}
}
@IBOutlet weak var cardSelector: UISegmentedControl!
@IBAction func cardSetSelected(_ sender: UISegmentedControl) {
cardEngine.updateCardSelection(cardSet: sender.selectedSegmentIndex)
}
}
答案 0 :(得分:0)
如果每次加载新视图时都创建cardDatabase的新实例,它当然会将cardDatabase实例的未初始化变量(selectedCardNum&amp; selectedCardArray)设置为nil。相反,你可以 - 要么将cardDatabase转换为Singleton类,所以只有一次实例生成并通过调用cardDatabase.shared来传递,这将使值保持可用(因为该实例被保留),如下所示:
class cardDatabase {
private static var instance : cardDatabase? = nil
private static var __once: () = {
instance = cardDatabase()
}
class var shared:cardDatabase{
get{
_ = cardDatabase.__once
return instance!
}
}
//Add your vars and functions below
}
- 或者如果您不关心继承,请将所有内容设置为静态,这样您就不需要创建任何cardDatabase实例,并且它们的值将保留在类级别(cardDatabase.selectedCardArray,cardDatabase。 selectedCardNum),像这样:
class cardDatabase {
public static func updateCardSelection(cardSet: Int){
print("updateCardSelection called")
switch cardSet {
case 0:
selectedCardArray = cardSetOne
selectedCardNum = 0
case 1:
selectedCardArray = cardSetTwo
selectedCardNum = 1
case 2:
selectedCardArray = cardSetThree
selectedCardNum = 2
default:
print("Debug Error - Card set not recognised")
}
print(selectedCardNum)
}
public static var selectedCardNum: Int!
public static var selectedCardArray: Array<String>!
//Create arrays for each card dataset
public static let cardSetOne = [
"Word 1",
"Word 2",
"Word 3",
"Word 4",
"Word 5",
"Word 6",
"Word 7",
"Word 8",
"Word 9"
]
public static let cardSetTwo = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
]
public static let cardSetThree = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i"
]
}