Return 来自另一个视图的文本错误,缺少参数参数
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
}
当我尝试在 SettingsView 中显示 welcomeView() 时出现错误
“调用中缺少参数 'currentAuthStat' 的参数”
我尝试将 @State var 添加到 SettingsView,但随后在 ContentView 中进行调用时出现错误,当我将其添加到 ContentView 时,它希望我将其添加到 App{} @main 页面中,然后我收到一个错误 app does不符合 View().
如何将此解释文本传递给另一个视图?
你应该学习和学习@State和@Binding的使用,因为它是基础
使用 SwiftUI。这是修复您收到的错误的可能方法:
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:currentAuthStat
被用户更改的情况:
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
}
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 页面中,然后我收到一个错误 app does不符合 View().
如何将此解释文本传递给另一个视图?
你应该学习和学习@State和@Binding的使用,因为它是基础 使用 SwiftUI。这是修复您收到的错误的可能方法:
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:currentAuthStat
被用户更改的情况:
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
}