未调用NSMenuItem操作

时间:2019-05-11 00:14:47

标签: swift macos cocoa appkit nsmenuitem

标题说明了一切:单击或等效键时,不会调用NSMenuItem的动作objcDeleteCurrentSelection。项目和功能是用作应用程序窗口菜单的NSMenu的一部分。

我试图将目标设置为nil和self(声明功能的NSMenu)。我还尝试过使用Any或AnyObject之类的参数。 NSMenuItem已正确启用或禁用。

仅供参考:objcDeleteCurrentSelection的实现调用了Swift协议扩展功能。

class OptionsMenu : NSMenu, NSMenuDelegate, OptionsControllerDelegate {

    private var disposeBag = DisposeBag()
    private var deleteItem: NSMenuItem? = nil

    required init(coder decoder: NSCoder) {
        super.init(coder: decoder)
        item(withTitle: "Edit")?.isEnabled = true
        deleteItem = item(withTitle: "Edit")?.submenu?.item(withTitle: "Delete")
        deleteItem?.action = #selector(objcDeleteCurrentSelection)
        delegate = self
        self.onSelectionChange.subscribe { [weak self] in
            guard let it = $0.element else { return }
            let enableDelete = !it.0.isEmpty || !it.1.isEmpty
            self?.deleteItem?.isEnabled = enableDelete
        }.disposed(by: disposeBag)
    }

    @objc func objcDeleteCurrentSelection() {
        deleteCurrentSelection()
    }

}
```

0 个答案:

没有答案