What is the correct subclass to link storyboard and swiftUI?

浪子不回头ぞ 提交于 2020-01-06 08:07:12

问题


I have created the subclass below to link my swiftui code to my storyboard. The goal is to have a vstack with text containers in it display inside a ContainerView. I am not sure if I am using the right class: NSViewController? I do not get any errors, but the code does not display how I want it to. Mostly, The swiftui does not display inside the window that shows up when I run the app.

import SwiftUI

class termu: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    @IBSegueAction func waka(_ coder: NSCoder) -> NSViewController? {
        return NSHostingController(coder: coder, rootView: ContentView())
    }

}

回答1:


Here is the simplest MyViewController which you can specify for new controller scene in IB for your storyboard as custom class in Identity Inspector. (The controller scene and segue creation in IB as usual).

I selected Sheet segue for demo

import Cocoa
import SwiftUI

class MyViewController: NSHostingController<ContentView> {

    @objc required dynamic init?(coder: NSCoder) {
        super.init(coder: coder, rootView: ContentView())
    }
}

struct ContentView: View { // This SwiftUI view is just for Demo
    var body: some View {
        VStack {
            Text("I'm SwiftUI")
                .font(.largeTitle)
                .padding()
                .background(Color.yellow)
        }
        .frame(width: 400, height: 200)
    }
}


来源:https://stackoverflow.com/questions/59550548/what-is-the-correct-subclass-to-link-storyboard-and-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!