officer_Krupke
officer_Krupke

Reputation: 27

Return Text from another view Error, missing argument parameter

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?

Upvotes: 0

Views: 150

Answers (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
} 

    

Upvotes: 1

Related Questions