Questions about SwiftUi

Hello, I need help with something I can’t find out. What I want to do is with the card game of Chris I want when the player score hits 20, automatically it will turn to another view. I created and if statement, but I don’t know how to tell the program to lunch another view when those conditions meet. I would appreciate your help thank you. The best I could do was to put an image but I couldn’t create another view https://photos.app.goo.gl/HqpWp9cnbfizQBzi9

1 Like

So do you want to show a card like you have in the attached image or do you want to present another full sized View on top of the existing one?

I want to present another full sized View on top of the existing one

OK, are you familiar with presenting a sheet functionality?

No, I am new learning and I was experimenting and I wanted to add that view when exactly the player count is a certain number

The .sheet functionality in SwiftUI enables another view to be slid into View. It is animated into view and by default slides up from the bottom.

I can run you through the process but it will take time to describe it.

Oh, could you tell me the code please, It would be totally fine for me to later on search because I would have the information

Firstly, have a look at the contents of this link to Paul Hudsons site. This is the basics of how a sheet is presented.
https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets

2 Likes

In your case there is bit more to it but see if you can understand what is happening in that description. As an exercise, create a new SwiftUI project and plug that code in.

Okey, thank you a lot! I was really frustrating because I even didn’t know what to search, but with what you give it is a great start, Thank you! It is almost new year, happy end of the year

And a Happy New Year to you Hose.

I’ll write up a few notes and post that in a few moments.

1 Like

If you implement the .sheet option then the following may be of assistance.

So ideally your code would be monitoring the score is some way and what you would do is set a trigger to true in order to cause the sheet to be presented. Similar to the way Paul Hudson describes his very small example.

For example, you might have a State variable like this:

@State private var showingSheet = false

Your code monitoring the score would set that to true when you score reached 20.

Create your view that you want to present and at the bottom of ContentView on the outer NavigationView, or Stack… within body you would add:

.sheet(isPresented: $showingSheet) {
    YourView()
}

When ContentView detects a change of State of a variable it triggers a refresh so with showingSheet set to true the .sheet code is executed and your other View is presented modally.

If you wanted it to do something on return then you would code it like:

.sheet(isPresented: $showingSheet, onDismiss: yourFunction) {
    YourView()
}

yourFunction would be defined after the body code for example:

func yourFunction() {
    // Perform some operations here
}

In the View you present there are a few settings required in order to dismiss it depending on what you intend to do within that View.

You need the following property
@Environment(\.presentationMode) var presentationMode

It would help if you added a NavigationView and at the bottom of that you could add a Button to exit when you are finished. like this:

.navigationBarItems(
    trailing: Button("Done", action: dismiss)
)

That button would appear up at the top right of your View in a navigation Bar.

and then add a function after the end of body like the following which will cause the View to close.

func dismiss() {
    presentationMode.wrappedValue.dismiss()
}
2 Likes

Thank you a lot! I finally did it. I am sorry for not answering earling but I am going to travel. Happy new year and thank you for the help.

Well done @Hose_Anyel. Happy New Year.