我试图根据布尔变量的值在viewController中加载两个不同的字符串数组之一。布尔值需要由可以从2个viewController调用的segue传递,其中一个将boolean设置为false,而另一个设置为true。
我试过将prepareForSegue(segue :)方法与performSegue(withIdentifier :)结合使用。设置了所有情节提要剧情链接和标识符。我还尝试将目标viewController嵌入到navigationController中,并调整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:)。
答案 0 :(得分:0)
在TestViewController
中,从
useHomework
的声明。
var useHomework = Bool()
遵循以下条件:
var useHomework: Bool?
并通过在使用位置展开“ useHomework”对其进行适当的更改。