NSCollectionViewItem 按钮动作 没有效果
NSCollectionViewItem button action No effect
点击按钮无效
希望好心人帮我看看原因
下面是我的代码
class cell_word_list_1: NSCollectionViewItem {
static let item = NSUserInterfaceItemIdentifier(rawValue: "cell_list_id_1")
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
let rootView = NSView()
rootView.wantsLayer = true
rootView.layer?.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
let button = NSButton()
button.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
button.action = #selector(click)
rootView.addSubview(button)
self.view = rootView
}
@objc func click() {
print("------> button click")
}
}
如果没有目标,操作将被发送到第一响应者和响应者链。这不起作用,因为该项目不在响应者链中。解决方法:设置按钮的目标:
button.target = self
点击按钮无效 希望好心人帮我看看原因 下面是我的代码
class cell_word_list_1: NSCollectionViewItem {
static let item = NSUserInterfaceItemIdentifier(rawValue: "cell_list_id_1")
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
let rootView = NSView()
rootView.wantsLayer = true
rootView.layer?.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
let button = NSButton()
button.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
button.action = #selector(click)
rootView.addSubview(button)
self.view = rootView
}
@objc func click() {
print("------> button click")
}
}
如果没有目标,操作将被发送到第一响应者和响应者链。这不起作用,因为该项目不在响应者链中。解决方法:设置按钮的目标:
button.target = self