【问题标题】:Sending data from CollectionView to CollectionView two with NotificationCenter使用 NotificationCenter 将数据从 CollectionView 发送到 CollectionView 二
【发布时间】:2021-12-03 13:43:49
【问题描述】:

我有一个单元格,其中包含一些属性,例如 pfp(Image) mainImage(Image) description(String) 和一个单元格中的一个like 按钮,单击那个like 按钮后,我想注册一个通知并在该通知中收到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
    }

我尝试使用第一个 CollectionViewCell 的值创建一个变量 aaa,然后将其放入一个数组中,但它似乎不起作用 任何解决方案都将适用 如果您需要任何类型的附加信息来解决此问题,请在 cmets 中告诉我,我会添加。 谢谢。

【问题讨论】:

  • 通过共享模型对象传递数据可能是一种更简单的方法。如果您想使用通知,则可以使用 userInfo 属性将引用附加到通知
  • @Paulw11 我该如何附加?
  • 您应该更好地为 collectionViewcell 创建一个委托协议,并为您传递变量 aaa 的委托创建一个委托。将协议添加到视图控制器并实现该方法。创建单元格时,将其委托设置为视图控制器 2。
  • @PtitXav 我想在通知中心做我的工作

标签: ios swift uicollectionview notificationcenter


【解决方案1】:

您遗漏了处理通知的一些重要部分,请查看文档以更熟悉其工作原理。

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

话虽如此,您可以通过以下方法实现您想要的。

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
    }
}

【讨论】:

  • 您的代码在init() 函数中返回错误'super.init' isn't called on all paths before returning from initializer,我搜索了该错误并尝试通过添加required init?super.init(nibName: nil, bundle: nil) 来修复它,但现在它返回一个fatalError我创建于required init
  • 我假设你的类中有一些未初始化的属性(?)。我提供的代码示例可以“按原样”使用。另一件事,您不应该将代码复制/粘贴到您自己的代码库中,您应该使用此处提供的相同模式自行编写。
【解决方案2】:

有一种简单的方法来创建易于访问的数据,通过通知中的创建变量和集合视图两个。必须有整个单元格。,像 Facebook 之类的有问题。

【讨论】:

  • 你能提供一些代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2017-10-14
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多