gpt4 book ai didi

swiftui - PresentationMode 触发器 "Variable X used before being initialized"

转载 作者:行者123 更新时间:2023-12-05 09:31:07 25 4
gpt4 key购买 nike

Error messages我正在尝试为 SwiftUI 应用程序编写可重用的快速选择字段编辑器。下面的代码仅显示错误 Variable 'self.fieldValue' used before initialized如果@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>存在。如果我删除它, View 编译正常。任何人都知道为什么 presentationmode 环境变量导致这个以及如何解决它?我需要 presentationmode env var,这样我就可以在用户单击按钮时快速弹出 View 。

@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

@State var viewName = "Edit Field"
@Binding var fieldValue: Int
@State var fieldName: String = ""
var options: [Int]

init(title: String, field: String, value: Binding<Int>, list: [Int]) {
viewName = title
fieldName = field
_fieldValue = value
options = list
}

完整 View 代码,见下文。

import SwiftUI

struct CommonNumberEditView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

@State var viewName = "Edit Field"
@Binding var fieldValue: Int
@State var fieldName: String = ""
var options: [Int]

init(title: String, field: String, value: Binding<Int>, list: [Int]) {
viewName = title
fieldName = field
_fieldValue = value
options = list
}

var body: some View {
ZStack {
Image("sky")
.resizable()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.opacity(0.5)

VStack(alignment: .center, spacing: 5)
{
JumpLogNumberField(label: self.fieldName, val: self.$fieldValue)

ScrollView {

VStack(alignment: .center, spacing: 5) {
ForEach(options, id: \.self) { item in
Group {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text(String(item))
.foregroundColor(.black)
.padding(20)
}
.frame(minWidth:0, maxWidth: .infinity)
.background(Color.white.opacity(0.6))
}
}
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .top)
}
.padding(.top, 40)
.padding(.leading, 40)
.padding(.trailing, 40)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)

}.navigationBarTitle(viewName, displayMode: .inline)
}
}

struct CommonFieldEditView_Previews: PreviewProvider {

static var previews: some View {
CommonNumberEditView(title: "Edit Field", field: "Altitude", value: .constant(5000), list: [ 13500, 10000, 5500, 3500 ])
}
}

最佳答案

这是固定的部分代码:

struct CommonNumberEditView: View {
@Environment(\.presentationMode) var presentationMode

@State private var viewName: String
@Binding var fieldValue: Int
@State private var fieldName: String
var options: [Int]

init(title: String, field: String, value: Binding<Int>, list: [Int]) {
_viewName = State(initialValue: title)
_fieldName = State(initialValue: field)
_fieldValue = value
options = list
}

// ...

使用 Xcode 13/iOS 15 测试

关于swiftui - PresentationMode 触发器 "Variable X used before being initialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69103529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com