简体   繁体   中英

Sending data from CollectionView to CollectionView two with NotificationCenter

I've a cell with some properties like pfp (Image) mainImage (Image) description (String) and a like button in one cell, after clicking that like button, I want to register a notification and recieve that notification on CollectionView two in that notification I must have a whole cell, Something like a facebook share button, whenever I share(like) a post, that post appends on my profile, I want to write same logic but instead of share I've a like button

//CollectionViewCell where like button is located
    @IBOutlet weak var profilePictureImage: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var mainImage: UIImageView!
    @IBOutlet weak var desc: UILabel!
    @IBOutlet weak var logo: UIImageView!
    @IBOutlet weak var likeButton: UIButton!
    func setup(with myArray: feedPage){
        self.nameLabel.text = myArray.pfName
        self.desc.text = myArray.description
        self.profilePictureImage.image = myArray.pfp
        self.logo.image = myArray.pfp
        self.mainImage.image = myArray.mainImage
        self.likeButton.setImage(UIImage(named: "like"), for: UIControl.State.normal)
    }
    @IBAction func likeButoon(_ sender: Any) {
        NotificationCenter.default.post(name: Notification.Name("AddToFavorites"), object: nil)
        var aaa = self.nameLabel.text
    }
//CollectionViewController two where I need to recieve a notification and configure second view controller cell
var test:[String] = []
        NotificationCenter.default.addObserver(self, selector: #selector(notificationRecieved), name: Notification.Name("AddToFavorites"), object: nil)
    }
    
    @objc func notificationRecieved(){
        self.test = aaa     //error is here xcode can't find `aaa` in scope
    }

I tried creating a variable aaa with the value of the first CollectionViewCell and then putting that into an array but it doesn't seem to work any solution will be appericated if you need any kind of addition info to solve this problem please let me know in the comments and I will add. Thank you.

You are missing some importants parts of handling notifications, please take a look in the docs to get more familiar with how it works.

https://developer.apple.com/documentation/foundation/notificationcenter

With that being said, you could achieve what you want with the following approach.

extension Notification.Name {
    static let AddToFavorites = Notification.Name("add_to_favorites")
}

class SomeView: UIView {

    @IBOutlet weak var nameLabel: UILabel!

    @IBAction func likeButoon(_ sender: Any) {
        let name = self.nameLabel.text
        // The name value must be sent with the posted notifaction, this enables observers to get the value from the received notification object.
        NotificationCenter.default.post(name: .AddToFavorites, object: name)
    }
}

class SomeController {

    init() {
        NotificationCenter.default.addObserver(self, selector: #selector(notificationRecieved), name: .AddToFavorites, object: nil)
    }

    @objc func notificationRecieved(notification: Notification){
        // Retrieve the name value from the notification object.
        guard let name = notification.object as? String else {
            return
        }
        // Do something with name
    }
}

There is easy way to create easy access data, passing through the create variable and collection view two in the notification. must have the whole cell.,something is wrong like Facebook to etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM