Learn Courses My Dashboard

Protocol and delegate property gets erased

Okay so I just set up a basic protocol delegate, and it works… kinda. when the protocol gets activated, the delegate runs its function and assigns a property (array = [1,2,3]) Then the segue goes through and the delegate view controller’s initializer kicks in and reassigns the array (array = ) sorta defeating the whole point. this is a shortened version of the code that you can just cut paste into a single view controller file and run, its all programmatic. you can use bug tags to see when you press the button it does assign the values but then you can see the result at the end is printed as 0 and

import UIKit

protocol getTheDefaults {
func gatherDefaults()
}

class ViewController: UIViewController {

let defaults = UserDefaults.standard
var getTheDefaultsDelegate: getTheDefaults?

lazy var quickStartButton: UIButton = {
    let myButton = UIButton()
    myButton.translatesAutoresizingMaskIntoConstraints = false
    myButton.backgroundColor = .cyan
    myButton.addTarget(self, action: #selector(quickStartButtonPressed), for: .touchUpInside)
    return myButton
}()

override func viewDidLoad() {
    super.viewDidLoad()
    defaults.setValue(4, forKey: Constants.CHOSEN_ARRAY_PASSED)
    defaults.setValue([13,12,11,10,9,8,7,6,5,4,3,2], forKey: Constants.PASSING_ARRAY)
    view.addSubview(quickStartButton)
    quickStartButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    quickStartButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    quickStartButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
    quickStartButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
}

@objc func quickStartButtonPressed() {
    //        set Flash card vc as delegate
    getTheDefaultsDelegate = FlashCardViewController()
    //        call the delegate method which assigns values to its properties
    getTheDefaultsDelegate!.gatherDefaults()
    //      segue to flash card vc
    let vc = FlashCardViewController()
    present(vc, animated: true, completion: nil)
}

}

class FlashCardViewController: UIViewController, getTheDefaults {

let defaults = UserDefaults.standard
var chosenArrayPassed: Int = 0
var passingArrayOfLettersSwitchedOff = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()
    print(chosenArrayPassed)
    print(passingArrayOfLettersSwitchedOff)
}


func gatherDefaults() {
    //retrieve chosenArrayPassed & passingArrayOfLettersSwitchedOff from the defaults
    chosenArrayPassed = defaults.object(forKey: Constants.CHOSEN_ARRAY_PASSED) as? Int ?? 2
    
    let ifEmptyArray: [Int] = []
    passingArrayOfLettersSwitchedOff = defaults.object(forKey: Constants.PASSING_ARRAY) as? [Int] ?? ifEmptyArray
}

}

struct Constants {
static var BUTTON_TO_VC_SEGUE = “buttonToVCSegue”
static var CHOSEN_ARRAY_PASSED = “chosenArrayPassed”
static var PASSING_ARRAY = “passingArrayOfLettersSwitchedOff”
static var FLASHVC_IDENTIFIER = “x”
}

solution: get rid of the protocol which only passes information backwards through the navigation controller, and install this override prepare for segue

override func prepare(for segue: UIStoryboardSegue, sender: Any ?) {

if segue.identifier == BUTTON_TO_VC_SEGUE {
let vc = segue.destination as! FlashCardViewController
//code to execute
}
}