【问题标题】:SwiftUI pass parameter to view static functionSwiftUI 传参查看静态函数
【发布时间】:2021-09-20 02:05:02
【问题描述】:

我有以下代码,我必须确保传递错误消息,如果没有传递任何内容,则将一般消息作为错误。

问题是我必须将它传递给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()
    }
}

【问题讨论】:

  • 如果你的 cmets/error 是英文的,你会在这里找到更好的成功
  • 这不适用于 SwiftUI。你不能像那样在 View 中调用方法。您可以共享一个 ViewModel 并让 ViewModel/Observable 在 View 中显示错误,但不允许您尝试做的事情。试试Apple SwiftUI Tutorials

标签: swift xcode macos swiftui static


【解决方案1】:

将默认错误消息设置为 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)
    }
}

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多