简体   繁体   English

使用 NotificationCenter 将数据从 CollectionView 发送到 CollectionView 2

[英]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我有一个单元格具有一些属性,如pfp (Image) mainImage (Image) description (String) 和一个单元格中的类似按钮,单击该按钮后,我想注册一个通知并在CollectionView two上收到该通知通知我必须有一个完整的单元格,就像一个 Facebook 分享按钮,每当我分享(喜欢)一个帖子时,该帖子会附加在我的个人资料上,我想写相同的逻辑,但我有一个喜欢按钮而不是分享

//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.我尝试使用第一个 CollectionViewCell 的值创建一个变量aaa ,然后将其放入一个数组中,但它似乎不起作用,如果您需要任何类型的附加信息来解决此问题,请告诉我任何解决方案评论,我会补充。 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 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.必须有整个单元格。,像Facebook这样的东西有问题等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一个 collectionView 传递到另一个 collectionView - Passing data from one collectionView to other collectionView 将数据从Collectionview传递到UIcollectionResuableview - pass data from Collectionview to UIcollectionResuableview CollectionView prepareForSegue不将图像数据发送到DetailView - CollectionView prepareForSegue Not Sending Image Data to DetailView 将collectionView数据发送到另一个View Controller中的出口 - Sending collectionView data to outlets in another view controller 具有发送图像的collectionView didSelectItemAtIndexPath - collectionView didSelectItemAtIndexPath with sending images 通过将“ ID”作为参数发送,将数据从一个collectionView单元格快速传递到另一collectionview控制器中的数据4 - passing data from one collectionView cell to other collection view controller in swift 4 by sending “ID” as a parameter 将数据从 Tableview 传递到 CollectionView SWIFT - passing data from Tableview to CollectionView SWIFT 从 Firestore 检索数据后,CollectionView 未重新加载 - CollectionView not reloading after retrieving data from Firestore 从firestore中读取数据并显示在collectionView中 - Read data from firestore and display in collectionView 如何从解析中过滤数据并在collectionview中显示 - How to filter data from Parse and display in a collectionview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM