BellRinging
BellRinging

Reputation: 401

How can I init a content with a parameter under .fullScreenCover in IOS14

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

I need to init a view with a parameter and pass it to .fullScreenCover, I can do it with TesFullScreen.init without problem, but it will error if I init the view with parameter, here is the sample code. Hope someone can help. Thanks

Without parameter is working

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

But with parameter is not working

     .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")
    }
}

full coding: https://github.com/BellRinging/ForBugsFix.git

Upvotes: 4

Views: 1394

Answers (1)

BellRinging
BellRinging

Reputation: 401

I Got it finally. Need to change it like this

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

Upvotes: 11

Related Questions