Нажатие кнопки Навигация до DetailView не работает в SwiftUI - PullRequest
0 голосов
/ 27 марта 2020

У меня есть интерфейс с несколькими кнопками. Я хочу перейти к подробному виду по нажатию кнопки. Впервые он успешно перемещается, но когда я возвращаюсь к представлению 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()
    }
}

1 Ответ

0 голосов
/ 27 марта 2020

Известная ошибка в XCode и симуляторах с SwiftUI, которая на самом деле прекрасно работает на реальных устройствах. Вы не можете два раза подряд сделать sh одно и то же представление.

Например, предположим, что у вас есть 3 строки (# 1, # 2, # 3), каждая с NavigationLink, вы не сможете в pu sh # 1, вернись и pu sh # 1 снова.

Если вы запустите # 1, # 2, а затем, # 1 снова, это работает.

Опять же, это только на симуляторе. Попробуйте тот же код на реальном устройстве, это будет хорошо!

Надеюсь, это поможет!

ОБНОВЛЕНИЕ: ошибка исправлена ​​в XCode 11.4. Теперь вы можете переходить несколько раз в один и тот же вид! Хорошая работа Apple;)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...