gpt4 book ai didi

swiftui - 如何在 IOS14 的 .fullScreenCover 下使用参数初始化内容

转载 作者:行者123 更新时间:2023-12-04 16:39:33 43 4
gpt4 key购买 nike

.fullScreenCover(isPresented: <#T##Binding#>, 内容: <#T##() -> View#>)

我需要使用参数初始化 View 并将其传递给.fullScreenCover,我可以使用TesFullScreen.init 毫无问题,但是如果我使用参数初始化 View 会出错,这是示例代码。希望有人可以提供帮助。谢谢

无参数工作

     .fullScreenCover(isPresented: self.$showFullScreen, content: TesFullScreen.init)

但是参数不起作用

     .fullScreenCover(isPresented: self.$showFullScreen, content: TesFullScreen(closeFlag: self.$showFullScreen, game: 1))

内容 View

import SwiftUI
struct ContentView: View {

var game : Int = 1
@State var showFullScreen : Bool = false

var body: some View {
NavigationView {
VStack {
Spacer()
Button(action: { self.showFullScreen = true }) {
Text("Show Full Screen")
}
Spacer()
}
.navigationBarTitle("TextBugs", displayMode: .inline)
}
.fullScreenCover(isPresented: self.$showFullScreen, content: TesFullScreen.init)
// .fullScreenCover(isPresented: self.$showFullScreen, content: TesFullScreen(game: 1))
}
}

TesFullScreen

struct TesFullScreen: View {

init(game : Int){
print(game)
}

init(){
print("No parameter work")
}

var body: some View {
Text("Full Screen")
}
}

完整编码: https://github.com/BellRinging/ForBugsFix.git

最佳答案

我终于明白了。需要这样改

.fullScreenCover(isPresented: self.$showFullScreen){
TesFullScreen(game: 1)
}

关于swiftui - 如何在 IOS14 的 .fullScreenCover 下使用参数初始化内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63989062/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com