Home > Blockchain >  Notification center can't recieve a notif
Notification center can't recieve a notif

Time:10-17

I've button inside Collection View Cell Whenever a person clicks that button, I want to register a notif, create variable name and set its value to the cell's property nameLabel, after that I want to send that notification to my second Collection View and set that Collection View's cell nameLabel.text to name string which I sent from first Collection View

if you need any addition code let me know in the comments

extension Notification.Name {
    static let AddToFavorites = Notification.Name("add_to_favorites")
}
// button inside first collection view cell
    @IBAction func likeButoon(_ sender: Any) {
        print("Button works fine")
        let name = self.nameLabel.text    // first collection view cell's property nameLabel
        NotificationCenter.default.post(name: .AddToFavorites, object: name)
    }
// second collection view controller
    var string1:String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(notificationRecieved), name: .AddToFavorites, object: nil)
    }
    @objc func notificationRecieved(notification: Notification){
        guard let name = notification.object as? String else {
            return
        }
        string1 = name
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionViewFavorites.dequeueReusableCell(withReuseIdentifier: "favoritesCell", for: indexPath) as? CollectionViewCellFavorites
        cell?.nameLabel.text = string1        // Second collection view cell's property nameLabel
        return cell!
    }

CodePudding user response:

You need to refresh

string1 = name
collectionView.reloadData()
  • Related