UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 35: edutainment app challenge - Switching between View's - need help.

Forums > 100 Days of SwiftUI

Hello everyone again I'm struggling with Day 35 and one of the things I'd like to understand is - How to switch between the views ?

I tried to make a simple layout to check this "Manual" way of switching (using closures as Paul noted in challange requirements) but it doen't work. Very simple idea : We have a plain screen with MainView, where we have a Button with action: that redirects us to SecondView. There we have the same Button with action: that redirects us to MainView. So basically we can create 10 different views containing some Buttons each of which can redirect us to one or another View we attached to closure in a Button.

But it doesn't work somehow. I tap on Button and no response. So would you mind to give hint what am I missing ?

Big thaks !

p.s. I know later I will discover advanced approaches , but for now I'd like to understand how to make this in a "manual" "stupid" way.

struct MainView: View {

    @State private var someString = ""
//    func secondViewTest() -> some View {
//        return SecondView()
//    }
    var body: some View {
        VStack {
            Form {
                Section(header: Text("Header")) {
                    TextField("type something here", text: $someString)
                    Text("Some text here")
                    Spacer()
                    Button("second view ->") { SecondView() }
                    //Button("move ->") { secondViewTest() } 
                    // that's second option. I tried to do this through 
                    // function but result is the same
                }
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        VStack {
            Button("main view <-") { MainView() }
        }
    }
}
struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

2      

@Bnerd  

You don't want to use a NavigationLink?

2      

@Bnerd thanks for reply and your suggestion!

Am I mistaken but I don't think NavigationLink has been covered before Day:35 ? :O

If it works, sure I can try and use it. But I'd like to know how to do this with Closure or Function as Paul suggested for our(student's) particular knowledge level at day 35. Otherwise everything else is kinda "cheating".

It's great to know more advanced techniques but I'd like to know "simple/primitive" solution too.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

@Bnerd  

Correct me if I am wrong but the challenge in Day 35, requires you to break down your view to smaller views, not navigation. Am I missing something? What he meant is that instead of having a 100 lines contentview, you can create separate smaller views and call them within your content view.

2      

@Bnerd , maybe I didn't get it right, I won't argue.

I took it as: a part of the challange is to make separate Views instead of making everything in one, default "ContentView" page . And in theory for that you need more Structs which represent separate views : / Isn't it ?

2      

@Bnerd  

You can still make separate Views in separate files :) Making the view in a separate file or making the view in a struct below your content view struct is the same thing!

2      

The reason your button isn't working is because it simply calls the initializer SecondView() in your button's action closure, but doesn't have any code to actually display the view that is created from it on the screen. So your button's action is basically "Create a view of type SecondView and then do nothing further with it."

You will learn many ways of showing additional views in future projects. But for this project, I used a VStack with of couple of if statement to accomplish it. This way, the views are created directly within the body of ContentView so that they will be shown on the screen, instead of being created in a Button's action closure.

struct ContentView: View {
    @State private var gameStarted = false
    @State private var difficulty = 0
    @State private var numberOfQuestions = 0

    var body: some View {
        ZStack {
            if gameStarted {
                GameView(difficulty: difficulty, numberOfQuestions: numberOfQuestions, newGame: newGame)
            }

            if !gameStarted {
                SettingsView(startGame: startGame)
            }
        }
    }

    func startGame(difficulty: Int, numberOfQuestions: Int) {
        self.difficulty = difficulty
        self.numberOfQuestions = numberOfQuestions
        gameStarted = true
    }

    func newGame() {
        gameStarted = false
    }
}

(I just tested and in the latest version of XCode I was actually able to remove the ZStack and use an if/else statement instead of 2 if statements. I don't think that I was able to do that when I first created the project for some reason though.)

2      

@FlyOstrich, thanks man!

Actually, it's because of you I created this topic. I was looking for help with solutions for day 35 Challenge and I've found only 2 topics with enough amount of code to make my brains undestand something. One of them was your's.

Even though it was not a full code for the working( you managed to get this option with changing views through functions with the help of @GreenAmber if I'm not mistaken) app I still could take something from your efforts. And here I wanted to clarify that option but as we can see not a lot of people willing to make a contribution. That's ok, no one owe anything to anyone.

Now I know that important part in that "technique" was usage of Boolean condition and if/else. They've been making all that stuff work ! And now you confirmed that in your reply. I appreciate !

I know already about NavigationLink. As you say there will be other methods. But it's never bad to know a little more I guess. Especially when a super slow guy like me tries to achieve success in programming but it's still super difficult and confusing and I'm constantly in 1 step to give up with a thought: "What's the point to continue if I already struggle with solutions being at the beginning of this course? And it's gonna be even more complicated and confusing later with every day".

p.s. The other guy was the only one to show here his full working solution code made in 1 single View. And guess what ? - No one even commented his great job and efforts. Topic was completely ignored and left without replies. I felt shame he didn't get even a drop of attention.

2      

Well, I think people usually try to avoid showing the full finished code for their projects in these forums because if you just give away all the answers, it doesn't give other people a chance to actually learn for themselves rather than just copy/paste some code.

However, sometimes it can be helpful to see the answer in order to study it and solidify your understanding of something. So, it is a fine line to dance along.

Some people look at these forums every day, and try to answer everyone that they can. Some people only stop by once per week, or maybe less often. So it can sometimes take a long time to get answers to your questions that you are looking for. You also have to remember that most of the people who do look at these forums are people who are currently working their way through the 100 days course themselves. So, they may not feel confident enough in their own knowledge of the subjects to be able to provide answers to other people's questions. I try to answer questions when I feel confident enough about my answers to help someone. But I don't always know the answers myself.

But, I wouldn't give up just because you are struggling with what you are learning right now. You've already made it 1/3 of the way through the course at least. Plus, the more you learn, the easier it becomes to progress with the future projects. Don't feel too bad about yourself if it takes you more than one day to complete 1 day of course material either. I have often spent multiple days on trying to make sure I fully understand something in this course. (Especially on the challenge days.)

3      

@ngknm14, OK so just for what you said , a plain simple way to present a new View

struct ContentView: View {
    @State private var showMeTheView = false    <----change state for any view updates
    var body: some View {
        Button("Go to Demo") {
            showMeTheView.toggle()   <----- change the state 
        }
        if showMeTheView {
            DemoView()   <--------you reach here now
        } 
    }
}

2      

I like to think of small chunks of view code as Lego bricks. Once you create a sweet, reusable Lego brick you can reuse it several places in your code. But soon enough, you'll be reusing the brick in other applications.

See -> Using Lego to Develop Views

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.