I just started learning Swift UI and have been Introduced into XCODE a few days ago. I am able to make a couple things thanks to my past experience but I'm very confused on how to switch Screens on a Button click. There are posts saying those screens are called "ViewControllers" so thats how I am referencing them now. This is urgent so all help is appreciated.
How do I switch ViewControllers from a Button?
This is urgent so all help is appreciated.
Urgent means you want to publish an app ? If so, take care, from the point you start, you may have a longer learning curve than expected.
I understand you use SwiftUI.
ViewControllers are used with UIKit, not in basic SwiftUI.
To switch screens, you have to:
- create swiftUI Views:
struct FirstView: View {
var body: some View {
// Button will be here
}
}
struct SecondView: View {
var body: some View {
Text("Hello, I'm the second View")
}
}
- use a NavigationView and navigationLink to go to SecondView
struct FirstView: View {
var body: some View {
NavigationView {
Button(action: {
print("button tapped")
},
label: {
NavigationLink(destination: SecondView()) {
Text("Go to Second View")
.bold()
.frame(width: 280, height: 50)
.background(Color.init(red: 0.818, green: 0.688, blue: 0.095))
.foregroundColor(.white)
.cornerRadius(10)
}
})
}
}
}
Thanks, im not trying to publish but urgent in the sense I need to take care of that part quickly