簡體   English   中英

如何以編程方式向 macOS 添加根視圖控制器?

[英]How do you add a root view controller to macOS programmatically?

我想弄清楚如何在 macOS 上設置根視圖控制器。 這有點令人困惑,我也遇到了一些錯誤。 我確實遵循了一些 iOS 指南,並嘗試查看有關 Mac 指南的其他一些帖子。 但我正在處理這些錯誤。 在 Info.plist 中,我的主故事板文件也設置為“Main”,我應該刪除它嗎? 我是 macOS 方面的新手。

我正在使用 AppKit 而不是 Apple 最新的 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'

這是我的視圖控制器。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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