![](/img/trans.png)
[英]How to dismiss NSViewController when NSButton is clicked programmatically
[英]NSButton object in NSViewController not work
我正在學習編寫沒有xib的MacOS程序 ,我之前已經編寫過JavaScript。
ViewControllerTest.swift
class ViewControllerTest: NSViewController {
lazy var button2 = NSButton(frame: NSMakeRect(455, 100, 50, 20))
override func loadView() {
...
button2.title = "2"
self.view.addSubview(button2)
button2.target = self
button2.action = #selector(self.button2Action)
}
@objc public func button2Action () {
NSLog("Button 2")
}
}
窗口
class Window: NSWindow {
@objc public func button1Action () {
NSLog("button 1")
}
init() {
...
// Button 1
let button1 = NSButton(frame: NSMakeRect(455, 400, 50, 20))
button1.title = "1"
self.contentView!.addSubview(button1)
button1.target = self
button1.action = #selector(self.button1Action)
// add view
let viewController = ViewControllerTest()
self.contentView!.addSubview(viewController.view)
}
}
class WindowController: NSWindowController, NSWindowDelegate {
override func windowDidLoad() {
super.windowDidLoad()
self.window!.delegate = self
}
}
我單擊按鈕1,控制台輸出“按鈕1”,但按鈕2不起作用。 有什么想法嗎? 我已經在網上搜索很長時間了。 但是沒用。 關於如何實現這一目標的任何想法?
我當時在同一條船上,但我想我已經知道了。 把你的
let viewController = ViewControllerTest()
在init()
,它現在應該可以工作了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.