从另一个视图返回文本错误,缺少参数参数
Posted
技术标签:
【中文标题】从另一个视图返回文本错误,缺少参数参数【英文标题】:Return Text from another view Error, missing argument parameter 【发布时间】:2021-10-19 10:25:00 【问题描述】: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()。
如何将此解释性文本传递给另一个视图?
【问题讨论】:
【参考方案1】:你应该好好学习和学习@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
【讨论】:
如果我将此设置设置为 .enabled,然后将覆盖该设置为 .enabled,我希望返回当前设置,这取决于用户是否允许通知。 正如我在代码中提到的,“除非你更改currentAuthStat
,否则不需要Binding”。这意味着,如果您希望用户更改currentAuthStat
,请使用Binding
。我已经用这样一个例子更新了我的答案。
感谢您的帮助,谢谢。以上是关于从另一个视图返回文本错误,缺少参数参数的主要内容,如果未能解决你的问题,请参考以下文章
使用应用程序脚本在 Big Query 中创建视图 |错误:对 bigquery.tables.insert 的 API 调用失败并出现错误:缺少必需的参数(第 21 行