У меня есть интерфейс с несколькими кнопками. Я хочу перейти к подробному виду по нажатию кнопки. Впервые он успешно перемещается, но когда я возвращаюсь к представлению root и снова нажимаю кнопку, я не перемещаюсь.
Я следовал всем учебным пособиям и вижу тот же код, который использовал, но не понимаю, в чем проблема.
Состояние переменной @State var google: Int? = 0
не задано в классе просмотра struct ContentView: View
Полный код root Просмотр
struct ContentView: View {
@State var username: String = ""
@State var password: String = ""
@State var selection: Int? = nil
@State var google: Int? = 0
@State var buttton1: Int? = nil
var body: some View {
NavigationView{
VStack{
Spacer()
VStack (alignment: .center, spacing: 20){
Text("Quickly find and book a\n Doctors visit today")
.bold()
.font(.title)
.foregroundColor(.white)
.multilineTextAlignment(.center)
TextField("Email", text: $username)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
TextField("Password", text: $password)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
NavigationLink(destination: DetailView(), tag: 3, selection: self.$buttton1) {
EmptyView();
}
Button(action:{
self.buttton1 = 3;
print("Login");
}){
Text("Sign in")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
.cornerRadius(3)
.foregroundColor(Color.white)
}
Text("Forgot password?")
.foregroundColor(Color.white)
}
.padding(20)
HStack {
VStack { Divider().background(Color.white) }
Text("or")
.foregroundColor(Color.white)
VStack { Divider().background(Color.white) }
}
.padding(20)
VStack(alignment: .leading, spacing: 20){
Button(action:{
print("Search and sign up user");
}){
Text("Search and sign up user")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.white)
.border(Color.white, width: 1)
.cornerRadius(3)
}
NavigationLink(destination: DetailView(), tag: 2, selection: self.$google) {
Button(action:{
self.google = 2;
print("Sign in with Google");
}){
HStack {
/*Image(systemName: "")
.font(.title)*/
Text("Sign in with Google")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.black)
.background(Color.white)
.cornerRadius(3)
}
}
}
/*NavigationLink(
destination: DetailView()){
Text("Click Me")
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.black)
.foregroundColor(Color.white)
}*/
NavigationLink(destination: DetailView(), tag: 1, selection: $selection) {
//EmptyView()
Button(action:{
//self.tag = 1;
self.selection = 1;
print("Sign in with Apple");
},label: {
Text("Sign in with Apple")
})
.frame(minWidth: 0, maxWidth: .infinity,minHeight: 50, maxHeight: 50)
.background(Color.black)
.cornerRadius(3)
.foregroundColor(Color.white)
}
}
.padding(20)
}
.background(Color.blue)
.edgesIgnoringSafeArea(.all)
}
}
}
struct ContentView: View {
@State var username: String = ""
@State var password: String = ""
@State var selection: Int? = nil
@State var google: Int? = 0
@State var buttton1: Int? = nil
var body: some View {
NavigationView{
VStack{
Spacer()
VStack (alignment: .center, spacing: 20){
Text("Quickly find and book a\n Doctors visit today")
.bold()
.font(.title)
.foregroundColor(.white)
.multilineTextAlignment(.center)
TextField("Email", text: $username)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
TextField("Password", text: $password)
.padding(.horizontal)
.foregroundColor(.white)
.accentColor(.white)
Divider()
NavigationLink(destination: DetailView(), tag: 3, selection: self.$buttton1) {
EmptyView();
}
Button(action:{
self.buttton1 = 3;
print("Login");
}){
Text("Sign in")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
.cornerRadius(3)
.foregroundColor(Color.white)
}
Text("Forgot password?")
.foregroundColor(Color.white)
}
.padding(20)
HStack {
VStack { Divider().background(Color.white) }
Text("or")
.foregroundColor(Color.white)
VStack { Divider().background(Color.white) }
}
.padding(20)
VStack(alignment: .leading, spacing: 20){
Button(action:{
print("Search and sign up user");
}){
Text("Search and sign up user")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.white)
.border(Color.white, width: 1)
.cornerRadius(3)
}
NavigationLink(destination: DetailView(), tag: 2, selection: self.$google) {
Button(action:{
self.google = 2;
print("Sign in with Google");
}){
HStack {
/*Image(systemName: "")
.font(.title)*/
Text("Sign in with Google")
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.foregroundColor(Color.black)
.background(Color.white)
.cornerRadius(3)
}
}
}
/*NavigationLink(
destination: DetailView()){
Text("Click Me")
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.black)
.foregroundColor(Color.white)
}*/
NavigationLink(destination: DetailView(), tag: 1, selection: $selection) {
//EmptyView()
Button(action:{
//self.tag = 1;
self.selection = 1;
print("Sign in with Apple");
},label: {
Text("Sign in with Apple")
})
.frame(minWidth: 0, maxWidth: .infinity,minHeight: 50, maxHeight: 50)
.background(Color.black)
.cornerRadius(3)
.foregroundColor(Color.white)
}
}
.padding(20)
}
.background(Color.blue)
.edgesIgnoringSafeArea(.all)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}