按钮单击导航到DetailView在SwiftUI中不起作用



我有一个只有几个按钮的UI。我想在单击按钮时导航到详细信息视图。这是它第一次成功导航,但当返回到根视图并再次单击按钮时,我不会导航。

我已经学习了所有的教程,我可以看到我使用过的相同代码,但我不知道发生了什么问题。

变量的状态@State var google: Int? = 0已在视图类struct ContentView: View中被违抗

根视图的完整代码

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 an 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 an 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()
}
}

XCode和SwiftUI模拟器上的已知错误,实际上在实际设备上运行良好。同一视图不能连续推送两次。

例如,假设您有3行(#1、#2、#3(,每行都有NavigationLink,您将无法推送#1,然后返回并再次推送#1。

如果您运行#1、#2,然后再运行#1,它会起作用。

同样,它只是在模拟器上。在真实的设备上尝试相同的代码,会很好!

希望这能有所帮助!

更新:XCode 11.4中的错误已修复。您现在可以多次导航到同一视图!干得好苹果;(

最新更新