通知中心收不到通知
Notification center can't recieve a notif
我在里面有按钮 Collection View Cell
每当有人单击该按钮时,我想注册一个通知,创建变量 name
并将其值设置为单元格的 属性 nameLabel
,之后我想将该通知发送到我的第二个 Collection View
并将 Collection View
的单元格 nameLabel
.text 设置为我从第一个发送的 name
字符串Collection View
如果您需要任何附加代码,请在评论中告诉我
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!
}
您需要刷新
string1 = name
collectionView.reloadData()
我在里面有按钮 Collection View Cell
每当有人单击该按钮时,我想注册一个通知,创建变量 name
并将其值设置为单元格的 属性 nameLabel
,之后我想将该通知发送到我的第二个 Collection View
并将 Collection View
的单元格 nameLabel
.text 设置为我从第一个发送的 name
字符串Collection View
如果您需要任何附加代码,请在评论中告诉我
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!
}
您需要刷新
string1 = name
collectionView.reloadData()