MTJ
MTJ

Reputation: 1

Logic Problem with my Program - Beginner Level

I am trying to change the view from the first view to final view in the code below in SWIFTUI. I would expect to go to final page after clicking button C. This does not happen unless I click C then A or B. What am I doing wrong?


struct ContentView: View {
    @State private var c = false
    @State private var a_or_b: letter?
    
    enum letter {
        case a; case b
    }
    
    var body: some View {
        if c == true {
            FinalView(c: $c)
        } else {
            VStack {
                Text("First View")
                
                Button(action: {
                    a_or_b = .a
                }) {
                    Text("A")
                    
                }
                
                Button(action: {
                    a_or_b = .b
                }) {
                    Text("B")
                    
                }
                
                Button(action: {
                    c = true
                }) {
                    Text("C")
                    
                    
                }
            }
        }
    }
    
    
    struct FinalView: View {
        @Binding var c: Bool
        
        var body: some View {
            Text("Second View!")
        }
    }
  

Upvotes: 0

Views: 58

Answers (2)

programr
programr

Reputation: 41

Seems to be an issue with Canvas. It works just fine on the simulator.

A workaround I found to make it work in Canvas is wrapping the if-else in a VStack:

var body: some View {
   VStack {
      if c == true {
         FinalView(c: $c)
      } else {
         VStack {
            Text("First View")
                
            Button(action: {
               a_or_b = .a
            }) {
               Text("A")
            }
                
            Button(action: {
               a_or_b = .b
            }) {
               Text("B")
            }
                
            Button(action: {
               c = true
            }) {
               Text("C")
            }
         }
      }
   }
}

Upvotes: 0

J W
J W

Reputation: 914

It may a problem for @State update

You can just change like this:

import SwiftUI

struct ContentView: View {
    @State private var c = false

    var body: some View {
        if c {
            FinalView()
        } else {
          FirstView(c: $c)
        }
    }
}

struct FirstView: View {
  @Binding var c: Bool
  @State private var a_or_b: letter?
  
  enum letter {
    case a; case b
  }
  
  var body: some View {
    VStack {
      Text("First View")
      Button(action: {
        a_or_b = .a
      }) {
        Text("A")
      }
      Button(action: {
        a_or_b = .b
      }) {
        Text("B")
      }
      
      Button(action: {
        c = true
      }) {
        Text("C")
      }
    }
  }
}

struct FinalView: View {
  var body: some View {
    Text("Final View")
  }
}

#Preview {
  ContentView()
}

Upvotes: 0

Related Questions