【问题标题】:Getting sender on a dynamically created NSButton checkbox在动态创建的 NSButton 复选框上获取发件人
【发布时间】:2019-04-16 10:24:48
【问题描述】:

我正在以编程方式创建 NSTableViewCells 并添加带有复选框的 NSButton 作为其中一些的子视图。当复选框被切换时,如何获取触发选择器的发件人?这是我迄今为止所做的工作,但我为获取发件人所做的一切尝试均未成功。

func addCheckBox(cell: NSTableCellView){
    let checkbox = NSButton(checkboxWithTitle: text, target: Any?.self, action: #selector(selector))
    checkbox.setButtonType(NSButton.ButtonType.onOff)
    cell.addSubview(checkbox)

}

@objc func selector(){
    print("selector selected")
}

【问题讨论】:

    标签: swift macos cocoa selector nsbutton


    【解决方案1】:
    1. target 设置为self

      let checkbox = NSButton(checkboxWithTitle: text, target: self, action: #selector(selector))
      
    2. 使用传递一个参数的语法

      @objc func selector(_ sender : NSButton){
          print("selector selected", sender.state)
      }
      

    【讨论】:

    • 它可以编译,但是添加了参数的选择器似乎没有被调用。编辑:在我看了mouseyaniac的例子之后,我想出了我迷路的地方。我认为这段代码也能正常工作。
    【解决方案2】:

    以下代码对我有用

    class AppDelegate: NSObject, NSApplicationDelegate {
    
    let mainWindow: NSWindow = {
        let window = NSWindow(contentRect: NSMakeRect(300, 300, 700, 700), styleMask: .resizable, backing: .buffered, defer: false)
        window.isOpaque = true
        window.styleMask.insert(.miniaturizable)
        window.styleMask.insert(.titled)
        window.makeKeyAndOrderFront(nil)
        window.title = "My Playground"
        window.isMovableByWindowBackground = true
        return window
    }()
    
    lazy var tableView : NSTableView = {
        let tableView = NSTableView()
        let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Sample Column"))
        tableView.addTableColumn(column)
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
    
        mainWindow.contentView = tableView
        // Insert code here to initialize your application
    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }}
    

    扩展 AppDelegate: NSTableViewDataSource, NSTableViewDelegate { func numberOfRows(in tableView: NSTableView) -> Int { 返回 10 }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let rowView = NSView()
    
        let button = NSButton()
        button.bezelStyle = .texturedRounded
        button.setButtonType(.switch)
        button.target = self
        button.action = #selector(selectorName(_:))
        button.title = "\(row)th view"
        button.translatesAutoresizingMaskIntoConstraints = false
    
    
        rowView.addSubview(button)
        return rowView
    }
    
    @objc func selectorName(_ sender: NSButton)
    {
        print("The state of \(sender.title) is \(sender.state)")
    }}
    

    【讨论】:

      猜你喜欢
      • 2015-01-16
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多