我有一个带有几个按钮的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 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()
}
}
答案 0 :(得分:0)
带有SwiftUI的XCode和模拟器上的已知错误实际上在实际设备上可以正常运行。 您不能在同一视图中连续两次按下
。例如,假设您有3行(#1,#2,#3),每行都有一个NavigationLink,则您将无法推动#1,请再次返回并再次推动#1。
如果您先运行#1,#2,然后再次运行#1,则它会起作用。
同样,它只是在模拟器上。在真实的设备上尝试相同的代码,那会很好!
希望这会有所帮助!
更新:该错误已在XCode 11.4中修复。您现在可以多次导航到同一视图!苹果干得好;)