@State属性不按预期更改其值,直到我添加`onChange(of:)

发布于 2025-01-22 14:16:37 字数 2088 浏览 0 评论 0 原文

import SwiftUI

struct ContentView: View {
    @State private var openSheet = false
    @State private var foo = 0
    
    var body: some View {
        Button {
            setFoo(100)
            openSheet = true
        } label: {
            Text("Open Sheet")
        }
        .padding()
        .frame(width: 400, height: 300, alignment: .center)
        .sheet(isPresented: $openSheet) {
            Text(String(foo))
                .padding()
                .frame(width: 200, height: 150, alignment: .center)
        }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}

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

上述代码运行时,结果为“ 0”,而不是“ 100”。但是,如果我添加了“。

import SwiftUI

struct ContentView: View {
    @State private var openSheet = false
    @State private var foo = 0
    
    var body: some View {
        Button {
            setFoo(100)
            openSheet = true
        } label: {
            Text("Open Sheet")
        }
        .padding()
        .frame(width: 400, height: 300, alignment: .center)
        .sheet(isPresented: $openSheet) {
            Text(String(foo))
                .padding()
                .frame(width: 200, height: 150, alignment: .center)
        }
        .onChange(of: openSheet) { newValue in
        }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}

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

“

知道为什么会发生这种情况?

import SwiftUI

struct ContentView: View {
    @State private var openSheet = false
    @State private var foo = 0
    
    var body: some View {
        Button {
            setFoo(100)
            openSheet = true
        } label: {
            Text("Open Sheet")
        }
        .padding()
        .frame(width: 400, height: 300, alignment: .center)
        .sheet(isPresented: $openSheet) {
            Text(String(foo))
                .padding()
                .frame(width: 200, height: 150, alignment: .center)
        }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}

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

When above code ran, the result was "0" instead of "100". However, if I added ".onChange(of:)" function, result changed to "100".

result01

import SwiftUI

struct ContentView: View {
    @State private var openSheet = false
    @State private var foo = 0
    
    var body: some View {
        Button {
            setFoo(100)
            openSheet = true
        } label: {
            Text("Open Sheet")
        }
        .padding()
        .frame(width: 400, height: 300, alignment: .center)
        .sheet(isPresented: $openSheet) {
            Text(String(foo))
                .padding()
                .frame(width: 200, height: 150, alignment: .center)
        }
        .onChange(of: openSheet) { newValue in
        }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}

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

result02

Any idea why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-01-29 14:16:37

使用此处的许多@asperi答案之一:
a>

您可以使用这样的东西:

extension Int: Identifiable {
    public var id: Int { self }
}

struct ContentView: View {
    @State private var foo: Int?  // <-- here
    
    var body: some View {
        Button { setFoo(100) } label: { Text("Open Sheet") }
            .padding()
            .frame(width: 400, height: 300, alignment: .center)
            .sheet(item: $foo) { newFoo in    // <-- here
                Text(String(newFoo))
                    .padding()
                    .frame(width: 200, height: 150, alignment: .center)
            }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}

using one of the many @Asperi answers from here:
Sheet inside ForEach doesn't loop over items SwiftUI

you could use something like this:

extension Int: Identifiable {
    public var id: Int { self }
}

struct ContentView: View {
    @State private var foo: Int?  // <-- here
    
    var body: some View {
        Button { setFoo(100) } label: { Text("Open Sheet") }
            .padding()
            .frame(width: 400, height: 300, alignment: .center)
            .sheet(item: $foo) { newFoo in    // <-- here
                Text(String(newFoo))
                    .padding()
                    .frame(width: 200, height: 150, alignment: .center)
            }
    }
    
    private func setFoo(_ foo:Int) {
        self.foo = foo
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文