如何更新@State变量并将其传递给iOS 14?

问题描述

在iOS14中,以下SwiftUI代码iOS13.x中的工作方式不同

如果您在iOS 14设备上运行此命令,则“选择”变量尽管已在控制台中正确更改和打印,但不会传递到以.sheet()呈现的下一个视图中。

如果在初始视图中显示“选择”变量,它将按预期工作。

只是注释 // Text(“当前:(selection.rawValue)”)

如果您在iOS13.x设备上运行它,它将按预期运行,而无需显示正在更新的值。

import SwiftUI

struct ContentView:View {
    
    @State private var selection=MyView.TestEnum.ONE
    @State private var presentSheet=false
    
    var body: some View {
        vstack {
//            Text("Currently: \(selection.rawValue)")
            Button(action: {
                selection=MyView.TestEnum.TWO
                integer=2
                print("selecting: \(selection.rawValue)")
                presentSheet=true
            },label: {
                Text("Select TWO")
            })
            .sheet(isPresented: $presentSheet,ondismiss: {
                print("dismissed")
            }) {
                MyView(tenum: selection)
            }
        }
    }
}

struct MyView:View {
    enum TestEnum: String,Codable {
        case ONE="ONE"
        case TWO="TWO"
    }
    var tenum:TestEnum
    var body: some View {
        vstack {
            Text("enum: \(tenum.rawValue)")
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)