Swiftui presentationMode.wrappedValue.Disclose()工作不正常
我使用sheet方法来显示一个简单的表单,并将几个变量传递给它。问题是,如果我在更改传入的变量后单击执行.dismise()方法的按钮,它将不起作用。相反,如果我直接点击按钮,它会正常工作。 代码如下:Swiftui presentationMode.wrappedValue.Disclose()工作不正常,swiftui,Swiftui,我使用sheet方法来显示一个简单的表单,并将几个变量传递给它。问题是,如果我在更改传入的变量后单击执行.dismise()方法的按钮,它将不起作用。相反,如果我直接点击按钮,它会正常工作。 代码如下: struct EditProductForm: View { var listIndex : Int var product : Product @State var quantity: Int @State var productName : String
struct EditProductForm: View {
var listIndex : Int
var product : Product
@State var quantity: Int
@State var productName : String
@EnvironmentObject var data : Data
@Environment(\.presentationMode) var presentationModeEdit
func editProduct(){
self.data.editProduct(listIndex: self.listIndex, product: self.product, productName: self.productName, quantity: self.quantity)
}
var body: some View {
VStack{
Spacer()
VStack(spacing: 64){
Text("Edit Product")
TextField("Edit the name", text: $productName)
Picker(selection: $quantity, label: Text("Quantity")){
Text("OK").tag(Constants.Status.OK)
Text("Almost finished").tag(Constants.Status.ALMOST_NONE)
Text("Finished").tag(Constants.Status.NONE)
}.pickerStyle(SegmentedPickerStyle())
Button(action: {
self.editProduct()
self.presentationModeEdit.wrappedValue.dismiss()
}){
Text("Save")
}
}
Spacer()
}.padding(.horizontal)
}
}
我还检查了isPresented变量是否改变了值,当我单击按钮时,它实际上被切换了,但工作表仍保留在那里
以下是我使用表单的代码:
ForEach(self.list.content, id: \.self) { item in
Button(action: {
self.show_modal_edit[self.list.content.firstIndex(of: item)!] = true
}){
ProductCell(item: item)
}.sheet(isPresented: self.$show_modal_edit[self.list.content.firstIndex(of: item)!]){
EditProductForm(
listIndex: self.listIndex,
product: item,
quantity: item.quantity,
productName: item.productName
).environmentObject(self.data)
}
}
show_modal_edit是一个Bool列表,我检查了这些值,显然正确的值被传递到了.sheet()的isPresented字段。我已经设置了以下代码的测试版本,在将数据重命名为Datax后,我在ios 13.4和macos catalyst上运行良好 这指向editProduct()中的函数 可能是你问题的根源。具体来说,使用数据作为类型的名称。它似乎与system struct数据类型冲突。尝试将ObserveObject类重命名为其他类(我的测试中的Datax)
导入快捷界面
类Datax:ObservableObject{
@已发布变量xxx=“xxx”
func editProduct(列表索引:Int,产品:String,产品名称:String,数量:Int){
打印(“-->editProduct”)
}
}
结构ContentView:View{
var data=Datax()
@状态变量showEditProductForm=false
var body:一些观点{
VStack{
文本(“主视图”)
按钮(“EditProductForm”){
self.showEditProductForm.toggle()
}
}.sheet(显示:$showEditProductForm){
EditProductForm(列表索引:2,产品:“产品”,数量:1,产品名称:“产品”)
.environmentObject(self.data)
}
}
}
结构EditProductForm:视图{
@环境对象变量数据:Datax
@环境(\.presentationMode)变量PresentationModelEdit:绑定
变量列表索引:Int
var乘积:字符串
@状态变量数量:Int
@状态变量productName:字符串
func editProduct(){
self.data.editProduct(列表索引:self.listIndex,产品:self.product,产品名称:self.productName,数量:self.quantity)
}
var body:一些观点{
VStack{
垫片()
VStack(间距:64){
文本(“编辑产品”)
TextField(“编辑名称”,文本:$productName)
选择器(选择:$数量,标签:文本(“数量”)){
文本(“确定”)。标记(0)
文本(“几乎完成”)。标签(1)
文本(“完成”)。标签(2)
}.pickerStyle(SegmentedPickerStyle())
按钮(操作:{
self.editProduct()
self.presentationModeEdit.wrappedValue.discouse()文件
}){
文本(“保存”)
}
}
垫片()
}.padding(.卧式)
}
}
希望这有助于追踪您的问题。我已经设置了您代码的以下测试版本,在将数据重命名为Datax后,我在ios 13.4和macos catalyst上的所有操作都很好 这指向editProduct()中的函数 可能是你问题的根源。具体来说,使用数据作为类型的名称。它似乎与system struct数据类型冲突。尝试将ObserveObject类重命名为其他类(我的测试中的Datax)
导入快捷界面
类Datax:ObservableObject{
@已发布变量xxx=“xxx”
func editProduct(列表索引:Int,产品:String,产品名称:String,数量:Int){
打印(“-->editProduct”)
}
}
结构ContentView:View{
var data=Datax()
@状态变量showEditProductForm=false
var body:一些观点{
VStack{
文本(“主视图”)
按钮(“EditProductForm”){
self.showEditProductForm.toggle()
}
}.sheet(显示:$showEditProductForm){
EditProductForm(列表索引:2,产品:“产品”,数量:1,产品名称:“产品”)
.environmentObject(self.data)
}
}
}
结构EditProductForm:视图{
@环境对象变量数据:Datax
@环境(\.presentationMode)变量PresentationModelEdit:绑定
变量列表索引:Int
var乘积:字符串
@状态变量数量:Int
@状态变量productName:字符串
func editProduct(){
self.data.editProduct(列表索引:self.listIndex,产品:self.product,产品名称:self.productName,数量:self.quantity)
}
var body:一些观点{
VStack{
垫片()
VStack(间距:64){
文本(“编辑产品”)
TextField(“编辑名称”,文本:$productName)
选择器(选择:$数量,标签:文本(“数量”)){
文本(“确定”)。标记(0)
文本(“几乎完成”)。标签(1)
文本(“完成”)。标签(2)
}.pickerStyle(SegmentedPickerStyle())
按钮(操作:{
self.editProduct()
self.presentationModeEdit.wrappedValue.discouse()文件
}){
文本(“保存”)
}
}
垫片()
}.padding(.卧式)
}
}
希望这有助于解决您的问题。谢谢您的回答,但不幸的是,这对我来说并不奏效。我怀疑原因可能是editproduct方法,因为在另一个文件中,我调用了类似的方法,但disclose有效。也许这与我的“EditProductForm”在ForEach中这一事实有关。我在我的问题中添加了代码。谢谢你的回答,但不幸的是,这对我不起作用。我怀疑原因可能是editproduct方法,因为在另一个文件中,我调用了类似的方法,但disclose有效。也许这与我的“EditProductForm”在ForEach中这一事实有关。我在我的问题中添加了代码。
self.data.editProduct(listIndex: self.listIndex, product: self.product, productName: self.productName, quantity: self.quantity)
import SwiftUI
class Datax: ObservableObject {
@Published var xxx = "xxx"
func editProduct(listIndex: Int, product: String, productName: String, quantity: Int) {
print("---> editProduct")
}
}
struct ContentView: View {
var data = Datax()
@State var showEditProductForm = false
var body: some View {
VStack {
Text("main view")
Button("EditProductForm") {
self.showEditProductForm.toggle()
}
}.sheet(isPresented: $showEditProductForm) {
EditProductForm(listIndex: 2, product: "product", quantity: 1, productName: "productName")
.environmentObject(self.data)
}
}
}
struct EditProductForm: View {
@EnvironmentObject var data: Datax
@Environment(\.presentationMode) var presentationModeEdit: Binding<PresentationMode>
var listIndex: Int
var product: String
@State var quantity: Int
@State var productName: String
func editProduct() {
self.data.editProduct(listIndex: self.listIndex, product: self.product, productName: self.productName, quantity: self.quantity)
}
var body: some View {
VStack{
Spacer()
VStack(spacing: 64){
Text("Edit Product")
TextField("Edit the name", text: $productName)
Picker(selection: $quantity, label: Text("Quantity")){
Text("OK").tag(0)
Text("Almost finished").tag(1)
Text("Finished").tag(2)
}.pickerStyle(SegmentedPickerStyle())
Button(action: {
self.editProduct()
self.presentationModeEdit.wrappedValue.dismiss()
}){
Text("Save")
}
}
Spacer()
}.padding(.horizontal)
}
}