簡體   English   中英

NSViewController中的NSButton對象不起作用

[英]NSButton object in NSViewController not work

我正在學習編寫沒有xibMacOS程序 ,我之前已經編寫過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM