xcode的8迅速3 OSX夫特3 OSX button.addTarget
我有內myView.I創建按鈕想動作添加到每個它創建的按鈕的代碼。我相信你通常會使用button.addTarget,但這不適用於OSX任何想法?
感謝
import Cocoa
class MainWindowController: NSWindowController {
var buttonArray: Array<NSButton> = []
var buttonTitleArray: [String] = ["Answer1","Answer2","Answer3","Answer4"]
@IBOutlet weak var myView: NSView!
override func windowDidLoad() {
super.windowDidLoad()
var horizontalx = 50
//Create button Array and tag buttons
for i in 0..<buttonTitleArray.count{
let button = NSButton(frame: NSRect(x: horizontalx, y: 50, width: 100, height: 50))
button.tag = i
buttonArray.insert(button, at: i)
horizontalx = horizontalx + 100
}
//Adds Buttons to myView
for i in 0..<buttonTitleArray.count{
let button:NSButton = buttonArray[i]
button.title = buttonTitleArray[i]
myView.addSubview(button)
}
//Just for testing we can find tag
for i in 0..<buttonTitleArray.count{
let buttonFromArray = buttonArray[i]
let tag = buttonFromArray.tag
if (tag == 2){
print("found Tag")
}
}
}//EO Overide
}//EnD oF thE wORld