0
struct SettingsView: View {
    
    var body: some View {
         
         welcomeView()    
              //:Missing argument for parameter 'currentAuthStat' in call

     }
}

struct welcomeView: View {
    @Binding var currentAuthStat: UNNotificationSetting
    
    var body: some View {
        explanatoryText
    }
    
    
    private var explanatoryText: Text {
        let explanatoryText: Text
        switch currentAuthStat {
        case .enabled:
            explanatoryText = Text("Notifications are enabled")
            + Text(Image(systemName: "checkmark"))
        default:
            explanatoryText = Text("Notifications disabled ")
            + Text(Image(systemName: "x.squareroot")) 
        }
        return explanatoryText
    }//: explanatoryText
}

当我尝试在 SettingsView 中显示 welcomeView() 时,我收到错误“调用中的参数 'currentAuthStat' 缺少参数”我尝试将 @State var 添加到 SettingsView 但它给了我一个错误,让我在 ContentView 中进行调用,当我将它添加到 ContentView 它希望我在 App{} @main 页面中添加它,然后我收到错误应用程序不符合 View()。

如何将此解释性文本传递给另一个视图?

4

1 回答 1

1

you should study and learn well the use of @State and @Binding because it is fundamental to using SwiftUI. Here is a possible way to fix the error you get:

import SwiftUI

@main
struct TesttApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
struct ContentView: View {
    var body: some View {
        SettingsView()
    }
}
 
struct SettingsView: View {
    @State var currentAuthStat: UNNotificationSetting = .enabled // <-- here give it a value
    
    var body: some View {
        welcomeView(currentAuthStat: currentAuthStat) // <-- pass it to the welcomeView
    }
}

struct welcomeView: View {
    // unless you change currentAuthStat, there is no need for Binding
    @State var currentAuthStat: UNNotificationSetting // <-- here receive the value
    
    var body: some View {
        explanatoryText
    }
    
    private var explanatoryText: Text {
        let explanatoryText: Text
        switch currentAuthStat {
        case .enabled:
            explanatoryText = Text("Notifications are enabled")
            + Text(Image(systemName: "checkmark"))
        default:
            explanatoryText = Text("Notifications disabled ")
            + Text(Image(systemName: "x.squareroot"))
        }
        return explanatoryText
    }//: explanatoryText
}

EDIT1: case where currentAuthStat is changed by the user:

struct welcomeView: View {
    // if you want to change currentAuthStat, use a Binding
    @Binding var currentAuthStat: UNNotificationSetting // <--- here a Binding 
    
    var body: some View {
        VStack (spacing: 55){
            explanatoryText
            Button("disabled setting"){
                currentAuthStat = .disabled  // <--- here test change
            }
        }
    }
    
    private var explanatoryText: Text {
        let explanatoryText: Text
        switch currentAuthStat {
        case .enabled:
            explanatoryText = Text("Notifications are enabled")
            + Text(Image(systemName: "checkmark"))
        default:
            explanatoryText = Text("Notifications disabled ")
            + Text(Image(systemName: "x.squareroot"))
        }
        return explanatoryText
    }//: explanatoryText
} 

    
于 2021-10-19T10:47:21.980 回答