I'm trying to pass data forward from my HdVC to my FaVC with Notification Center using a button called addToFav and it does not seem to pass my data I call var item. I need a little help figuring it out a way to modify the code so my item can be passed and set to a variable currentFav I have in my FaVC.
HdVC
FaVC
HdVC
Code Block class HockeyDetailVC: UITableViewController { let imageController = FetchImage() var item: CurrentPlayers? /* This object value I want in FaVC */ override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(handleFavNotification(notification:)), name: .passFavNotification, object: nil) } @objc func handleFavNotification(notification:Notification){ if let theFav = notification.object as? CurrentPlayers { self.item = theFav } } @IBAction func addToFav(_ sender: Any) { let alert = UIAlertController(title: "Favourite Added 💙", message: "\(name.text ?? "") is added to favourites", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)) self.present(alert, animated: true, completion: nil) print("Favourite button Pressed") let passDataVC = FavouritesVC() /* I think passDataVC is supposed to be different because I use storyboard but I don't know what else it's supposed to be. */ self.present(passDataVC, animated: true, completion:nil) }
FaVC
Code Block class FavouritesVC: UITableViewController { var currentFav: CurrentPlayers? /*this is the variable I want to set my item too. */ var favArr = [CurrentPlayers]() override func viewDidLoad() { super.viewDidLoad() if currentFav == nil { self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none } else { NotificationCenter.default.post(name: .passFavNotification, object: self.currentFav) favArr.append(currentFav!) print(currentFav!) } }
I guess you need some storage independent from FavouritesVC.
For example:
HockeyDetailVC
FavouritesVC
For example:
Code Block class FavouritesManager { static let shared = FavouritesManager() var favArr: [CurrentPlayers] = [] func add(_ player: CurrentPlayers) { favArr.append(player) NotificationCenter.default.post( name: .passFavNotification, object: player ) } }
HockeyDetailVC
Code Block class HockeyDetailVC: UITableViewController { //... var item: CurrentPlayers? /* This object value I want in FaVC */ override func viewDidLoad() { super.viewDidLoad() } @IBAction func addToFav(_ sender: Any) { let alert = UIAlertController(title: "Favourite Added 💙", message: "\(name.text ?? "") is added to favourites", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default) {_ in if let favPlayer = self.item { FavouritesManager.shared.add(favPlayer) } }) self.present(alert, animated: true, completion: nil) print("Favourite button Pressed") } //... }
FavouritesVC
Code Block class FavouritesVC: UITableViewController { //var currentFav: CurrentPlayers? /*this is the variable I want to set my item too. */ var favArr: [CurrentPlayers] { FavouritesManager.shared.favArr } override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver( self, selector: #selector(handleFavNotification), name: .passFavNotification, object: nil) //Other settings... //... } @objc func handleFavNotification(notification: Notification) { tableView.reloadData() // Or something else... } //... }