0
votes

I'm trying to load one of two different string arrays in a viewController depending on a boolean variable's value. The boolean value needs to be passed by a segue that can be called from 2 viewControllers, one sets boolean to false, and the other true.

I've tried using prepareForSegue(segue:) method with performSegue(withIdentifier:). All storyboard segue links and identifiers are setup. I've also tried embedding the destination viewController in a navigationController and adjusting the segue code to reference the 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)")
                }
            }
        }
    }
}

The debug console always reports useHomework as false, when segueing from either class. When I set useHomework as true in testViewController's class initialisers, I get the expected result.

homeworkWords array contains the correct string values, which are added into homeworkWords and saved to userDefaults in the other (words) viewController that segues to testViewController.

homeworkWords is never loaded and saved to the words string array, as loadWords(fromWordsArray:) is never called in the boolean condition test.

1

1 Answers

0
votes

In TestViewController change the declaration of useHomework from

var useHomework = Bool()

to following:

var useHomework: Bool?

And make appropriate changes to 'useHomework' by unwrapping it where it is used.