如何在iOS14中使用.fullScreenCover下的参数初始化内容

时间:2020-09-21 08:55:40

标签: swiftui ios14

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

我需要使用参数初始化视图并将其传递给.fullScreenCover,我可以使用TesFullScreen.init毫无问题地完成此操作,但是如果我使用参数初始化视图,则会出错,这是示例代码。希望有人能帮忙。谢谢

无参数有效

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

但是带参数不起作用

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

ContentView

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

1 个答案:

答案 0 :(得分:1)

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

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