SwiftUI 传参查看静态函数

SwiftUI pass parameter to view static function

我有以下代码,我必须确保传递一条错误消息,如果未传递任何消息,则将一般消息视为错误。

问题是我必须将它传递给 static function 然后设置错误但我没有成功。

你能帮帮我吗?

ErrorView.showWindow(error: "La cartella già esiste.") error <-- "La cartella già esiste."
ErrorView.showWindow() error <-- "C'è stato un problema."
import SwiftUI

struct ErrorView: View {
    
    var error: String = "C'è stato un problema."
    
    var body: some View {
        HStack(alignment: .center) {
            Image(nsImage: NSApp.applicationIconImage)
                .resizable()
                .frame(width: 96, height: 96)
                .shadow(radius: 5)
                .padding(16)
                .padding(.horizontal, 6)
                .padding(.bottom, 22)
            
            VStack(alignment: .leading, spacing: 8) {
                VStack(alignment: .leading) {
                    Text("Git Repository")
                        .font(Font.title.weight(.thin))
                }
                .padding(.bottom, 8)
                
                VStack(alignment: .leading, spacing: 14) {
                    Text(error)
                }
                .font(.system(size: 11))
                .frame(maxHeight: .infinity)
                
                
            }
        }
        .padding(.top, 10)
        .padding(.bottom, 24)
        .padding(.horizontal, 16)
        .frame(width: 525, height: 200)
    }
    
    static func showWindow(error:String) {
        let viewController = NSHostingController(rootView: ErrorView())
        let windowController = NSWindowController(window: NSWindow(contentViewController: viewController))
        
        if let window = windowController.window {
            window.title = "Title"
            window.titleVisibility = .hidden
            window.titlebarAppearsTransparent = true
            window.animationBehavior = .alertPanel
            window.styleMask = [.titled, .closable]
        }
        
        windowController.showWindow(nil)
        NSApp.activate(ignoringOtherApps: true)
    }
}

struct ErrorView_Previews: PreviewProvider {
    static var previews: some View {
        ErrorView()
    }
}

将默认错误消息设置为 showWindow 函数而不是 ErrorView 错误变量。

另一个错误是您没有将错误消息传递给 ErrorView。

所以最终代码是

struct ErrorView: View {
    
    var error: String //<< Here
    
    var body: some View {
        HStack(alignment: .center) {
            Image(nsImage: NSApp.applicationIconImage)
                .resizable()
                .frame(width: 96, height: 96)
                .shadow(radius: 5)
                .padding(16)
                .padding(.horizontal, 6)
                .padding(.bottom, 22)
            
            VStack(alignment: .leading, spacing: 8) {
                VStack(alignment: .leading) {
                    Text("Git Repository")
                        .font(Font.title.weight(.thin))
                }
                .padding(.bottom, 8)
                
                VStack(alignment: .leading, spacing: 14) {
                    Text(error)
                }
                .font(.system(size: 11))
                .frame(maxHeight: .infinity)
            }
        }
        .padding(.top, 10)
        .padding(.bottom, 24)
        .padding(.horizontal, 16)
        .frame(width: 525, height: 200)
    }
    
    static func showWindow(error:String = "C'è stato un problema.") { //<< Here
        let viewController = NSHostingController(rootView: ErrorView(error: error)) //<< Here
        let windowController = NSWindowController(window: NSWindow(contentViewController: viewController))
        
        if let window = windowController.window {
            window.title = "Title"
            window.titleVisibility = .hidden
            window.titlebarAppearsTransparent = true
            window.animationBehavior = .alertPanel
            window.styleMask = [.titled, .closable]
        }
        
        windowController.showWindow(nil)
        NSApp.activate(ignoringOtherApps: true)
    }
}