我是 Swift 的初学者,我正在尝试通过通知中心启动一个功能。 “ViewController.swift”中的观察者调用函数reload
:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reload), name: NSNotification.Name(rawValue: "reload"), object: nil)
}
func reload(target: Item) {
print(target.name)
print(target.iconName)
}
...它有一个类参数Ítem
:
class Item: NSObject {
let name: String
let iconName: String
init(name: String, iconName: String) {
self.name = name
self.iconName = iconName
}
}
该通知是从“menu.swift”发布的:
class menu: UIView, UITableViewDelegate, UITableViewDataSource {
let items: [Item] = {
return [Item(name: "Johnny", iconName: "A"), Item(name: "Alexis", iconName: "B"), Item(name: "Steven", iconName: "C")]
}()
...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reload"), object: items[indexPath.row])
}
如何分配对象的值items[indexPath.row]
从'menu.swift'到函数的参数reload
在“ViewController.swift”中?