将 NSbutton 连接到从 Xib 加载 NSView 的方法的代码

Code to connect NSbutton to a method to load a NSView from Xib

我正在开发我的第一个应用程序。我有一些我想连接的 NSButtons,这样每个按钮都会将不同的子视图加载到现有的 NSView 中。我对此还是很陌生,我正在努力寻找最好的编码方式。

我有一个 class 来处理按钮操作,为了简洁起见,这里只显示一个:

class SelectorTest: NSObject {

@objc class func printButton1Pushed(_ sender: NSButton) {
print("button one pushed")

ContentView.showView1()

}
}

以上工作正常,按下按钮时字符串打印到控制台。但是它给出了 "Result of call to 'showView1()' is unused" 错误。我想我正在为在 ContentView class.

的 showview1() 方法中放入什么而苦恼

我创建了一个视图,一个 .xib 和一个 class 调用相同的东西。如果我添加为子视图和 运行 应用程序,这很好用。按下按钮时如何加载它?这是我目前的代码,按钮不加载视图:

class ContentView: NSView {

static let cView = NSView()


class func showView1() -> NSView {

    let view1 = TestView()
    cView.addSubview(view1.view)
    return cView
}
}

编辑:关于该项目的更多信息。我有一个主视图控制器。作为子视图添加到它,我有一个 NSStackview,包括左侧按钮的视图和右侧的视图,希望在其中显示 NSButtons 调用的每个视图。

经过更多阅读,我认为 "Result of call to 'showView1()' is unused" 错误意味着 "hey, you called this method, it returned an NSView for you, but you didn't use it for anything"。我卡在这部分了,如何更新视图以显示我刚刚创建的视图?

这里有一个简单的例子,说明我认为(我可能是错的)你想做什么,用故事板完成。如果您更喜欢以编程方式做事,那只需要多几行代码,但构建 IMO 的最简单方法是使用自动布局,这会使演示代码变得混乱。

import Cocoa

class ViewController: NSViewController {
    var stackedViews = [NSView]()

    @IBAction func showRed(_ sender: Any) {
        stackedViews[1].layer?.backgroundColor = NSColor.red.cgColor
    }
    @IBAction func showGreen(_ sender: Any) {
        stackedViews[1].layer?.backgroundColor = NSColor.green.cgColor
    }
    @IBAction func showBlue(_ sender: Any) {
        stackedViews[1].layer?.backgroundColor = NSColor.blue.cgColor
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let sView = view.subviews[0] as? NSStackView else {
            print("No StackView")
            return
        }
        for v in sView.arrangedSubviews {
            stackedViews.append(v)
        }
        stackedViews[0].wantsLayer = true
        stackedViews[0].layer?.backgroundColor = NSColor.black.cgColor
        stackedViews[1].wantsLayer = true
        stackedViews[1].layer?.backgroundColor = NSColor.purple.cgColor
       // Do any additional setup after loading the view.
    }


}

希望这对您有所帮助。

情节提要大纲如下所示:

情节提要如下所示:

结果如下所示:

给你。 (将其作为第二个答案,而不是编辑我之前的答案,因为之前的已经足够长了。我还是新来的,我希望这不违反任何规则。不要试图玩弄声誉的事情:)

主视图控制器仍然是从故事板生成的。

如果这有点冗长,我只是想展示所有的机器。毫无疑问,还有很多其他方法可以做同样的事情。



class ViewController: NSViewController {
// Initializing these objects here eliminates optionals
    let stackView = NSStackView(views: [makeView(.purple), makeView(.black)])
    let redButton = labeledButton("Red", action: #selector(showRed))
    let greenButton = labeledButton("Green", action: #selector(showGreen))
    let blueButton = labeledButton("Blue", action: #selector(showBlue))
// Action handlers are objective-c functions. @IBAction implies that
    @objc func showRed(_ sender: Any) {
        stackView.arrangedSubviews[1].layer?.backgroundColor = NSColor.red.cgColor
    }
    @objc func showGreen(_ sender: Any) {
        stackView.arrangedSubviews[1].layer?.backgroundColor = NSColor.green.cgColor
    }
    @objc func showBlue(_ sender: Any) {
        stackView.arrangedSubviews[1].layer?.backgroundColor = NSColor.blue.cgColor
    }
// Helper to make multiple buttons. Static so it can initialize instance properties
    class func labeledButton(_ stringValue: String = "", action: Selector) -> NSButton {
        let button = NSButton()
        button.action = action
        button.translatesAutoresizingMaskIntoConstraints = false
        button.bezelStyle = NSButton.BezelStyle.rounded
        button.title = stringValue
        return button
    }
// Helper to make multiple views. Also must be static
    class func makeView(_ color: NSColor) -> NSView {
        let vw = NSView()
        vw.translatesAutoresizingMaskIntoConstraints = false
        vw.wantsLayer = true
        vw.layer?.backgroundColor = color.cgColor
        return vw
    }

    private func createStackView() {
// Configure & add the stack view
        stackView.orientation = .horizontal
        stackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackView)
// Pin it to the enclosing view
        stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// Set widths of subviews, and pin them to the stack view
        stackView.arrangedSubviews[0].widthAnchor.constraint(equalToConstant: 100).isActive = true
        stackView.arrangedSubviews[1].widthAnchor.constraint(greaterThanOrEqualToConstant: 300).isActive = true
        stackView.arrangedSubviews[0].topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
        stackView.arrangedSubviews[1].topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true
        stackView.arrangedSubviews[0].bottomAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true
        stackView.arrangedSubviews[1].bottomAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true
 // Add the buttons to the left view
        stackView.arrangedSubviews[0].addSubview(redButton)
        stackView.arrangedSubviews[0].addSubview(greenButton)
        stackView.arrangedSubviews[0].addSubview(blueButton)
// Space them vertically, and center them horizontally
        redButton.centerXAnchor.constraint(equalTo: stackView.arrangedSubviews[0].centerXAnchor).isActive = true
        greenButton.centerXAnchor.constraint(equalTo: stackView.arrangedSubviews[0].centerXAnchor).isActive = true
        blueButton.centerXAnchor.constraint(equalTo: stackView.arrangedSubviews[0].centerXAnchor).isActive = true

        redButton.topAnchor.constraint(equalTo: stackView.arrangedSubviews[0].topAnchor, constant: 40).isActive = true
        greenButton.centerYAnchor.constraint(equalTo: stackView.arrangedSubviews[0].centerYAnchor).isActive = true
        blueButton.bottomAnchor.constraint(equalTo: stackView.arrangedSubviews[0].bottomAnchor, constant: -40).isActive = true

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        createStackView()
    }


}