手动创建nsbutton-调用功能 [英] create nsbutton manually - call function
问题描述
这个问题可能不重要,但是我现在搜索了几个小时,只是找不到合适的链接.
This question probably is trivial but I am searching for hours now and just dont find the fitting link.
我正在编写一个具有许多领域的棋盘游戏,例如国际象棋棋盘.我设法将所有字段都创建为带有for ... in和代码的按钮:
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)
按下按钮时,我要启动一个功能.这该怎么做!? 我只找到了C ++和Ios的代码,却没有找到Swift Macos的代码. 例如,这是为Ios提供的,但是我不知道如何将其更改为MacOS:
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)
推荐答案
您可以添加目标和操作,如下所示:
You can add target and action like this:
fled.target = self
fled.action = #selector(ButtonTest)
这篇关于手动创建nsbutton-调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!