0

I am trying to transfer my object from HockeyDetailVC to my FavouritesVC using a button but my object is nil when I reach my second VC FavouritesVC. Why is it like that when I set the variable in my firstVC with my func transferObj()?

HockeyDetailVC

var item: CurrentPlayers?

override func viewDidLoad() {
        super.viewDidLoad()
        gonnaLoadView()
        tableV.bounces = false
        tableV.alwaysBounceVertical = false
        favButton.layer.cornerRadius = 10
        print(item)    *//prints my current players object*
    }

   func transferObj() {
        let otherVC = FavouritesVC()
        otherVC.currentFav = item
        print(item).  *//prints my current player object*
    }

  @IBAction func addToFav(_ sender: Any) {
    transferObj()
    print("Favourite button Pressed")
}

FavouritesVC

  var currentFav: CurrentPlayers?

  override func viewDidLoad() {
    super.viewDidLoad()
    if currentFav == nil {
    //display nil
    self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
    print(favArr)    *//prints empty array*
    print(currentFav) *//nil*
    } else {
    favArr.append(currentFav!)
    print(favArr)
    }
}
  • 1
    `let otherVC = FavouritesVC()` creates a *new instance* of the controller, it does not give you a reference to the presented view controller. There are several similar Q&As already, see for example https://stackoverflow.com/q/47271711/1187415 (and also https://stackoverflow.com/q/5210535/1187415). – Martin R Feb 13 '21 at 07:47
  • How do you present `FavouritesVC`? – Roman Podymov Feb 13 '21 at 07:48

1 Answers1

1

As @Martin stated, let otherVC = FavouritesVC() creates a new instance of the controller, but it is not the instance that you will eventually display. So you are effectively setting the currentFav of a random FavouritesVC that will never actually be displayed, while the one you eventually do navigate to has it's currentFav property still unset.

To set the appropriate FavouritesVC instance, you need to access it in one of several ways (depending on how you present it). If it is through a segue, then you can reference it in the prepare(for segue: sender:) method. (When you create a Cocoa Touch Class file, the below method template is pre-populated. As it states, reference the new view controller using segue.destination.)

/*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

Alternatively, if you create and present the new view controller programmatically with something like

// 1.
let otherVC = storyboard?.instantiateViewController(withIdentifier: "yourFavouritesVCIdentifier")

// 2.

// 3.
self.show(otherVC, sender: self)

you can insert your otherVC.currentFav = item at line // 2..

Eric33187
  • 1,006
  • 7
  • 13