Search code examples
swiftswiftuinsviewcontrollerrepresentable

iosMath is crashing SwiftUI


I'm trying to build a Mac App using SwiftUI where I want to display Math using IosMath. I installed it using CocoaPods and I'm able to import it. But every Time I try to get to my View Containing the MTMathUILabel my App is crashing saying : 027055+0200 latextest[1709:84867] [General] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: latextest.Latex in bundle (null).

My code goes as following: In SwiftUI:

import SwiftUI

struct Mittel: View {
    var body: some View {
        VStack{
            Text("Das Mittel berechnet sich: ")
            Switcher()
        }
    }

}

calling my NSViewControllerRepresentable:

import AppKit
import SwiftUI


struct Switcher : NSViewControllerRepresentable {
    func makeNSViewController(context: Context) -> Latex {
        print("Test")
        return Latex()
    }

    func updateNSViewController(_ nsViewController: Latex, context: Context) {
    
    }
}

to load the View:

import AppKit
import iosMath

class Latex: NSViewController {

    //public var latexstring: String! = "\\frac{2}{3}"
    //let latexlabel = MTMathUILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let container1 = NSView(frame: CGRect(x: 0, y: 0, width: 200, height: 70))
        let label: MTMathUILabel = MTMathUILabel()
        label.latex = "x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}"

        label.frame = container1.frame
        container1.addSubview(label)
        self.view.addSubview(container1)
        }
}

Solution

  • Solution: There was no nib file because I wasn't using InterfaceBuilder... so I needed to change the Controller see

        override func viewDidLoad() {
        super.viewDidLoad()
    

    needed to be changed to.

    override func loadView() {
    self.view = NSView()