我正在开发我的第一个应用程序。我想连接一些NSButton,以便每个按钮将不同的子视图加载到现有的NSView中。我对此还很陌生,并且正在努力寻找最好的编码方法。
我有一个处理按钮动作的类,这只是为了简洁起见:
class SelectorTest: NSObject {
@objc class func printButton1Pushed(_ sender: NSButton) {
print("button one pushed")
ContentView.showView1()
}
}
上面的方法工作正常,当按下按钮时,字符串会打印到控制台上。但是,它给出“未调用'showView1()'的结果”错误。我想我正在为ContentView类的showview1()方法中的内容苦苦挣扎。
我创建了一个视图,一个.xib和一个叫做同一个东西的类。如果我添加为子视图并运行该应用程序,则此方法效果很好。按下按钮后如何加载?这是我到目前为止的代码,按钮不会加载视图:
class ContentView: NSView {
static let cView = NSView()
class func showView1() -> NSView {
let view1 = TestView()
cView.addSubview(view1.view)
return cView
}
}
编辑:有关该项目的更多信息。我有一个主视图控制器。作为子视图,我还添加了一个NSStackview,其中包括左侧按钮的视图和右侧视图,希望可以显示每个由NSButton调用的视图。
多一点阅读后,我认为“对'showView1()的调用结果未使用”的错误意思是,“嘿,您调用了此方法,它为您返回了一个NSView,但是您没有使用它为任何东西”。我被困在这部分上,如何更新视图以显示刚刚创建的视图?
答案 0 :(得分:0)
这是我想用情节提要完成的想法的一个简单示例(我可能错了)。如果您更喜欢以编程方式执行操作,那只是几行代码,但是构建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.
}
}
希望这会有所帮助。
故事板如下:
答案 1 :(得分:0)
您在这里。 (第二个答案,而不是编辑我的前一个答案,因为前一个答案已经足够长了。我在这里还很新,我希望这不违反任何规则。不要尝试打名声:)
主视图控制器仍从情节提要中生成。
如果这有点冗长,我只想展示所有机器。毫无疑问,还有许多其他方法可以做同样的事情。
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()
}
}