我是macOS编程的新手,我想为NSToolbarItem实现一个自定义类。每次按下该项时,我都想更改类的一个属性。下面是一个示例代码:
class CustomToolbarButton: NSToolbarItem
{
override init(itemIdentifier: String)
{
super.init(itemIdentifier: itemIdentifier)
super.target = self
super.action = #selector(reactToPress)
}
func reactToPress(sender: NSToolbarItem)
{
toggled_property = !toggled_property
print("Item pressed")
}
private(set) var toggled_property = true;
}此类被插入到情节提要的工具栏中。我已经确保将身份检查器中的类说明符更改为CustomToolbarButton。但是,该操作似乎从未触发过,因为“项目按下”从未出现在控制台输出中。
我还尝试通过以下方式声明"reactToPress“函数:
func reactToPress()
@objc func reactToPress()
@objc func reactToPress(sender: NSToolbarItem)但还是没有成功。
发布于 2019-09-13 02:15:04
对于项的目标,您需要一个非弱引用。试试这个:
// Define this in your class.
static let itemTarget = CustomToolbarButton(itemIdentifier: "myButton")
// and use it when setting the target in the constructor.
self.target = CustomToolbarButton.itemTarget从docs.swift.org“弱引用是一个引用,它不保持对它所引用的实例的强控制,因此不会阻止ARC处理被引用的实例。”
如果对该实例的所有引用都是弱引用,则会自动创建并释放该项。您至少需要一个非弱引用才能保持对实例的控制。
推荐阅读:https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
此外,正如您所说的,操作函数中需要@objc指令。这里有一些有用的链接。
“#selector”引用未公开给Objective-C '#selector' refers to a method that is not exposed to Objective-C的方法
请参见选择器表达式https://docs.swift.org/swift-book/ReferenceManual/Expressions.html
最后是一个关于Xcode 10.3和Swift 5的工作示例:
// Created by Juan Miguel Pallares Numa on 9/12/19.
// Copyright © 2019 Juan Miguel Pallares Numa. All rights reserved.
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// Strong reference.
var myWindowController = WindowController()
func applicationDidFinishLaunching(_ aNotification: Notification) {
myWindowController.makeWindowKeyAndOrderFront()
}
}
import Cocoa
class ToolbarController: NSObject, NSToolbarDelegate {
let identifiers = [NSToolbarItem.Identifier("myIdentifier")]
let toolbar = NSToolbar()
let toolbarItem: NSToolbarItem
override init() {
toolbarItem = NSToolbarItem(itemIdentifier: identifiers[0])
super.init()
toolbar.delegate = self
toolbarItem.label = "Print My Console Message"
toolbarItem.target = self
toolbarItem.action = #selector(
ToolbarController.toolbarAction(_:))
toolbarItem.image = NSImage(named: NSImage.applicationIconName)!
}
@objc func toolbarAction(_ sender: Any?) {
print("Hello world")
}
func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return identifiers
}
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return identifiers
}
func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return identifiers
}
func toolbar(_ toolbar: NSToolbar,
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
return toolbarItem
}
}
import Cocoa
class WindowController: NSWindowController {
var toolbarController = ToolbarController()
convenience init() {
let window = NSWindow(
contentViewController: ViewController())
self.init(window: window)
window.toolbar = toolbarController.toolbar
}
func makeWindowKeyAndOrderFront() {
window?.makeKeyAndOrderFront(nil)
}
}
import Cocoa
class ViewController: NSViewController {
override func loadView() {
view = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 300))
}
}发布于 2017-08-10 02:05:19
尝试符合Objective-C的语法
super.action = #selector(reactToPress(_:))和
func reactToPress(_ sender: NSToolbarItem)在Swift 4中,您必须显式添加@objc属性
@objc func reactToPress(_ sender: NSToolbarItem)https://stackoverflow.com/questions/45597665
复制相似问题