Switching Views in SwiftUI with a button.

How would I switch to a different view in SwiftUI with a press of a button? I've tried doing the method where pressing a button toggles a variable, but it just spits a bunch of errors. Also, would I need to make a new SwiftUI file, or could I incorporate the new view into the default ContentView.swift?

I've tried doing the method where pressing a button toggles a variable, but it just spits a bunch of errors.

You may be doing something wrong. Can you show your code?

would I need to make a new SwiftUI file, or could I incorporate the new view into the default ContentView.swift?

Anyway, as you like.
This is the code:
Code Block //
//  ContentView.swift
//  Computing History (1940s)
//
//  Created by _____ on 4/2/21.
//
import SwiftUI
struct ContentView: View {
    @State var showingEniacHistory = false
    var body: some View {
        if showingEniacHistory {
                ContentView()
            } else {
                EniacHistory()
            }
        }
        
        VStack {
            Text("Major Advances in Computing History - ENIAC & FRIENDS")
                .fontWeight(.bold)
                .multilineTextAlignment(.leading)
                .padding()
            Spacer()
            Text("Basic Info")
                .font(.subheadline)
                .fontWeight(.bold)
            Spacer()
            Text("The ENIAC was the first ever programmable computer. ENIAC stood for ''Electronic Numerical Integrator and Computer'' and it cost the government around $400,000 to build. It started comstruction in 1943, and was completed in 1945. It had 40 panels and took up a majority of the space of the 50 by 30 foot walls of the basement of the Moore School in Pensylvania. To say the least, It paved the way for a number of life altering devices. This app will inform you about the ENIAC and many other innovative and revolutionary computers.")
                .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            Spacer()
            Button("Next Page") {
                showingEniacHistory = true
            }
                
            
            
        }
    }
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}



Your code has several fundamental issues as a SwiftUI source:
  • The body of ContentView contains ContentView

(Are you trying sort of recursive view structure?)
  • VStack block is not in body, nor in any other methods or computed properties

(You should better keep your code always properly indented.)
Switching Views in SwiftUI with a button.
 
 
Q