Ios 如何使用通知中心将数据从FirstTableView发送到SecondTableView而不使用segue
当我试图使用NotificationCenter设计模式将一个数组从UITableView传递到另一个UITableView时,我遇到了一个问题(因为我在这两个UIViewController之间没有分段)。我不知道我做错了什么,但我在第二个视图控制器中没有收到任何数据 我的函数如下所示: *第一个VC-发送方控制器(从那里发送数据)*Ios 如何使用通知中心将数据从FirstTableView发送到SecondTableView而不使用segue,ios,swift,nsarray,nsnotificationcenter,Ios,Swift,Nsarray,Nsnotificationcenter,当我试图使用NotificationCenter设计模式将一个数组从UITableView传递到另一个UITableView时,我遇到了一个问题(因为我在这两个UIViewController之间没有分段)。我不知道我做错了什么,但我在第二个视图控制器中没有收到任何数据 我的函数如下所示: *第一个VC-发送方控制器(从那里发送数据)* class ProductsViewController: UIViewController{ var selectedProductsArray = [Pr
class ProductsViewController: UIViewController{
var selectedProductsArray = [Product]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Implement Notification Design Pattern to send data
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "productsToLoad"), object: selectedProductsArray)
print(selectedProductsArray) // Here I have some data in this array (Photo here: https://ibb.co/k8hoEy)
}
class CartViewController: UIViewController {
var productsInCartArray = [Product]()
// We retrieve data from "selectedProductsArray" and we append all the products into "productsInCartArray"
@objc func notificationRecevied(notification: Notification) {
productsInCartArray = notification.object as! [Product]
print(productsInCartArray)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Add observer to watch when something was changed in "selectedProductsArray"
NotificationCenter.default.addObserver(self, selector: #selector(notificationRecevied(notification:)), name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
print(productsInCartArray) // Output: []
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// We remove the observer from the memory
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
}
}
*第二视图控制器-接收器控制器(我将在其中接收数据)*
class ProductsViewController: UIViewController{
var selectedProductsArray = [Product]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Implement Notification Design Pattern to send data
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "productsToLoad"), object: selectedProductsArray)
print(selectedProductsArray) // Here I have some data in this array (Photo here: https://ibb.co/k8hoEy)
}
class CartViewController: UIViewController {
var productsInCartArray = [Product]()
// We retrieve data from "selectedProductsArray" and we append all the products into "productsInCartArray"
@objc func notificationRecevied(notification: Notification) {
productsInCartArray = notification.object as! [Product]
print(productsInCartArray)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Add observer to watch when something was changed in "selectedProductsArray"
NotificationCenter.default.addObserver(self, selector: #selector(notificationRecevied(notification:)), name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
print(productsInCartArray) // Output: []
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// We remove the observer from the memory
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
}
}
截图:
如果您正在阅读此文章,感谢您抽出时间 您需要从
CartViewController的视图中删除此项
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
let products = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray
当您在产品中发布时,卡不会显示,因此其中没有侦听器,除此之外,CartViewController
应在发布来自ProductsViewController
的任何数据之前至少打开一次
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
let products = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray
//
您可以完全删除NotificationCenter
工作,然后在CartViewController
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "productsToLoad"), object: nil)
let products = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray
注意:
不要为担心代码>展开它不会崩溃是的,这正是您所说的。如何解决此问题,因为我不想在FirstView和SecondView之间浏览两次以查看数据。谢谢你的回答!没有检索任何数据,当我从SecondTab返回到FirstTab时崩溃,出现以下错误:“无法将类型为'UINavigationController'(0x10FFFF18)的值强制转换为'ShoppingLand.ProductsViewController'(0x10d167668)。”现在不再崩溃,而是我的secondArray(“产品”)崩溃是空的,当我在购物车中添加1个项目时,在我的第一个数组中,该项目添加了2次。所以我添加了1台iPhoneX,我的阵列中会有2台iPhoneX。对不起,我在视图中调用了你的代码将消失…哈哈。只使用一行代码!!!非常感谢您的时间!!!Happy coding=DI回答了类似的问题@