create nsbutton manually - call function

别说谁变了你拦得住时间么 提交于 2020-01-06 04:35:26

问题


This question probably is trivial but I am searching for hours now and just dont find the fitting link.

I am programming a board game with many fields like a chess board. I managed to create all fields as buttons with for ... in and code like this:

let feld = NSButton()
feld.frame = CGRect(x: 1300+30*h, y: 20, width: 22, height: 22)
feld.image = NSImage(named: NSImage.Name("Mulleimer"))
arrayOfButtons.append(feld)

When a button is pressed I want to start a function. How to do this!? I only found code for C++ and Ios but not for Swift Macos. For example this one was given for Ios but I dont know how to change it to MacOS:

feld.Target(self, action: #selector(ButtonReiheDeaktivieren), for: .touchUpInside)

回答1:


You can add target and action like this:

fled.target = self
fled.action = #selector(ButtonTest)


来源:https://stackoverflow.com/questions/48102130/create-nsbutton-manually-call-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!