Is this intended @State var behaviour in iOS14 or is it a bug?

瘦欲@ 提交于 2021-02-08 02:04:46

问题


When I run the following code compiled for iOS14 on iOS14 sim or device, the updated @State variable "selection" doesn’t get passed to a view displayed as .sheet(… The exact code runs OK on iOS 13.5 sim or 13.6 device

The code runs as expected on iOS 14, if I place somewhere in the view a Text(“”) and display the variable being updated.

It seems that only if I display the value, the .sheet get's rendered again updating the MyView(tenum: selection) with the correct value.

In the code below just uncomment the line below to make it work in iOS14

// Text("Currently: (selection.rawValue)")

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
                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 {
        Text("enum: \(tenum.rawValue)")
    }
}

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

来源:https://stackoverflow.com/questions/63952133/is-this-intended-state-var-behaviour-in-ios14-or-is-it-a-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!