如何以编程方式将根视图控制器添加到macOS?

问题描述

我试图弄清楚如何在macOS上设置根视图控制器。这有点令人困惑,我也遇到了一些错误。我确实遵循了一些iOS准则,并尝试查看有关Mac准则的其他文章。但是我正在处理这些错误。在Info.plist中,我还将主故事板文件设置为“ Main”,是否应该删除文件?我是macOS方面的新手。

我使用的是AppKit,而不是苹果的最新SwiftUI库。所以这是我要处理的事情。非常感谢提供帮助的任何人! :)

这是我的AppDelegate文件

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject,NSApplicationDelegate {

var window: NSWindow?


func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
    
    let storyBoard = nsstoryboard(name: nsstoryboard.Name("Main"),bundle: Bundle.main)
    let homeViewController = storyBoard.instantiateController(withIdentifier: nsstoryboard.SceneIdentifier("ViewController")) as! ViewController
    NSApp.keyWindow?.contentViewController = homeViewController
    
}

func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
}


}

这是我得到的错误

2020-11-06 01:24:11.985119-0800 avengers[53735:1621559] Failed to set (contentViewController) user defined inspected property on (NSWindow): Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x600002a0f940 "NSTextView:0x7fecb041f8b0.centerX"> and <NSLayoutXAxisAnchor:0x600002a0c040 "NSView:0x7fecb0609d80.centerX"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.

2020-11-06 01:24:12.028071-0800 avengers[53735:1621559] Storyboard (<nsstoryboard:0x600003d08320 path='/Users/jazilzaim/Library/Developer/Xcode/DerivedData/avengers-gixxxwjnvqcwkreksxccdalagqme/Build/Products/Debug/avengers.app/Contents/Resources/Base.lproj/Main.storyboardc',initialController='NSWindowController-B8D-0N-5wS'>) doesn't contain a controller with identifier 'ViewController'

这是我的View Controller。

import Cocoa

class ViewController: NSViewController {

let labelTxt: NSText = {
    let text = NSText()
    text.string = "Hello"
    text.translatesAutoresizingMaskIntoConstraints = false
    return text
}()


override func viewDidLoad() {
    super.viewDidLoad()

    
    labelTxt.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    labelTxt.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    labelTxt.topAnchor.constraint(equalTo: view.topAnchor,constant: 150).isActive = true
    self.view.addSubview(labelTxt)
    
}

override var representedobject: Any? {
    didSet {
    // Update the view,if already loaded.
    }
}


}

解决方法

您以编程方式创建vc,因此请替换

let storyBoard = NSStoryboard(name: NSStoryboard.Name("Main"),bundle: Bundle.main)
let homeViewController = storyBoard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("ViewController")) as! ViewController

使用

let homeViewController = ViewController()

此外,您还应该在创建约束之前添加子视图

self.view.addSubview(labelTxt)
labelTxt.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
labelTxt.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
labelTxt.topAnchor.constraint(equalTo: view.topAnchor,constant: 150).isActive = true