我试图根据布尔变量的值在viewController中加载两个不同的字符串数组之一。布尔值需要由可以从2个viewController调用的segue传递,一个将boolean设置为false,另一个将true设置为true。
我试过对performPergue(withIdentifier :)使用prepareForSegue(segue :)方法。所有情节提要剧情链接和标识符均已设置。我也尝试过将目标viewController嵌入NavigationNavigator中,并调整segue代码以引用navigationController。
import UIKit
class homeworkViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var useHomework = Bool()
var homeworkWords = [String]()
override func viewDidLoad() {
super.viewDidLoad()
loadHomeworkWords()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(test))
}
func loadHomeworkWords() {
if let defaults = UserDefaults(suiteName: "group.co.uk.tirnaelectronics.hyperpolyglot.todayview") {
if var savedHomeWorkWords = defaults.object(forKey: "homeworkWords") as? [String] {
print("savedLanguages array is: \(savedHomeWorkWords)")
homeworkWords.removeAll()
homeworkWords = savedHomeWorkWords
savedHomeWorkWords.removeAll()
print("savedHomeWorkWords after removeAll: \(savedHomeWorkWords)")
print("loadHomeworkWords homeworkWords are: \(homeworkWords)")
}
}
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Test" {
if let testVC = segue.destination as? TestViewController {
testVC.useHomework = useHomework
}
}
}
@objc func test() {
if homeworkWords.isEmpty == true {
let ac = UIAlertController(title: "Add some homework!", message: nil, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
ac.addAction(okAction)
present(ac, animated: true)
} else {
useHomework = true
self.performSegue(withIdentifier: "Test", sender: self)
}
}
}
import UIKit
class TestViewController: UIViewController, UITextFieldDelegate {
var words: [String]!
var useHomework = Bool()
var homeworkWords = [String]()
var chosenLanguage = String()
let wordsString = "Words"
override func viewDidLoad() {
super.viewDidLoad()
print("useHomework is: \(useHomework)")
if useHomework == false {
loadWords(fromWordsArray: "words")
} else {
loadWords(fromWordsArray: "homeworkWords")
}
print("words just before shuffle: \(words!)")
words.shuffle()
print("words just after shuffle: \(words!)")
}
func loadWords(fromWordsArray: String) {
if let defaults = UserDefaults(suiteName: "group.co.uk.tirnaelectronics.hyperpolyglot.todayview") {
if fromWordsArray == "words" {
if var savedWords = defaults.object(forKey: "\(chosenLanguage)\(wordsString)") as? [String] {
print("chosen words in loadWords in TestViewcontroller are: \(savedWords)")
words = savedWords
savedWords.removeAll()
}
} else {
if var savedWords = defaults.object(forKey: "homeworkWords") as? [String] {
print("savedWords array is: \(savedWords)")
words = savedWords
savedWords.removeAll()
print("homeworkWords in loadWords in TestViewcontroller are: \(savedWords)")
}
}
}
}
}
从任何一个类中隔离时,调试控制台始终将useHomework报告为false。当我在testViewController的类初始化程序中将useHomework设置为true时,我得到了预期的结果。
homeworkWords数组包含正确的字符串值,这些字符串值已添加到homeworkWords中,并保存到与testViewController连接的其他(单词)viewController中的userDefaults中。
homeworkWords永远不会加载并保存到单词字符串数组,因为在布尔条件测试中永远不会调用loadWords(fromWordsArray :)。
最佳答案
在TestViewController
中,将useHomework
的声明从var useHomework = Bool()
以下:var useHomework: Bool?
并通过在“ useHomework”使用的地方对其进行包装来对其进行适当的更改。