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
}

when I try to display welcomeView() in the SettingsView I get the error "Missing argument for parameter 'currentAuthStat' in call" I tried adding @State var to SettingsView but then it gives me an error to make the call in ContentView and when I add it to ContentView it wants me to add that in the App{} @main page and then I get an error app does does not conform to View().

How can I pass this explanatoryText to another view?

1 Answers1

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
} 

    
  • If I set this setting to .enabled then that overrides that setting to .enabled, I was hoping to return the current setting which is dependent if the user allows notifications. – officer_Krupke Oct 19 '21 at 17:31
  • As I mentioned in the code, "unless you change `currentAuthStat`, there is no need for Binding". That means, if you want the user to change `currentAuthStat` then use a `Binding`. I have updated my answer with such an example. – workingdog support Ukraine Oct 19 '21 at 22:50
  • I appreciate the help, thank you. – officer_Krupke Oct 20 '21 at 10:14