使用 NotificationCenter 将数据从 CollectionView 发送到 CollectionView 二
Sending data from CollectionView to CollectionView two with NotificationCenter
我有一个单元格,其中包含一些属性,例如 pfp
(Image) mainImage
(Image) description
(String) 和一个单元格中的“赞”按钮,单击“赞”后按钮,我想注册一个通知并在该通知中的 CollectionView two
上接收该通知我必须有一个完整的单元格,类似于 facebook 共享按钮,每当我共享(喜欢)一个 post,那个 post 附加在我的个人资料上,我想写相同的逻辑但我没有分享我有一个喜欢的按钮
//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
,然后将其放入一个数组中,但它似乎不起作用
任何解决方案都会被应用
如果您需要任何类型的附加信息来解决此问题,请在评论中告诉我,我会添加。
谢谢。
您遗漏了处理通知的一些重要部分,请查看文档以更熟悉它的工作原理。
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
}
}
有一种简单的方法可以创建易于访问的数据,通过通知中的创建变量和集合视图两个。必须有整个单元格。Facebook 等有问题
我有一个单元格,其中包含一些属性,例如 pfp
(Image) mainImage
(Image) description
(String) 和一个单元格中的“赞”按钮,单击“赞”后按钮,我想注册一个通知并在该通知中的 CollectionView two
上接收该通知我必须有一个完整的单元格,类似于 facebook 共享按钮,每当我共享(喜欢)一个 post,那个 post 附加在我的个人资料上,我想写相同的逻辑但我没有分享我有一个喜欢的按钮
//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
,然后将其放入一个数组中,但它似乎不起作用
任何解决方案都会被应用
如果您需要任何类型的附加信息来解决此问题,请在评论中告诉我,我会添加。
谢谢。
您遗漏了处理通知的一些重要部分,请查看文档以更熟悉它的工作原理。
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
}
}
有一种简单的方法可以创建易于访问的数据,通过通知中的创建变量和集合视图两个。必须有整个单元格。Facebook 等有问题