【问题标题】:Init State var coming from another view , swiftui [duplicate]Init State var 来自另一个视图,swiftui [重复]
【发布时间】:2022-01-15 09:21:53
【问题描述】:

从视图 A 导航到视图 B,我正在传递一个 var show 。视图 A 不再可见。

我需要一个 @State var stateShow ,其初始值与从 A 传递给 B 的值 show 相同。

不使用 onAppear 怎么办?谢谢

struct B : View {

    public var show:Bool
    
    @State private stateShow : Bool
    
    init (_ show : Bool){
        self.show = show
    }
    
}

【问题讨论】:

  • 可能,但据我所知
  • _stateShow = State(initialValue: show)

标签: swiftui init


【解决方案1】:

init (_ show : Bool){

    self.show = show
    _stateShow = State(initialValue: show)
}

答案归功于 vadian,另一个类似的问题被问到了类似的答案,但条件略有不同:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多