Understangin @Binding & PresentationView в SwiftUI - PullRequest
0 голосов
/ 12 июня 2019

у меня есть 2 просмотра (основной и конфиг)

mainView с простым обратным отсчетом и configView с максимальным значением обратного отсчета ...

на главном экране есть кнопка Presentation для перехода к ConfigView

PresentationButton(Text("Config").color(.white)
                .padding(.all)
                .cornerRadius(20)
                , destination: ConfigView(timeInSeconds: .constant(String(time)))).frame( alignment: .trailing)

если я установил максимальное значение в configview, а затем в mainview изменилось значение de countview de configview .. снова появляется

struct MainView: View {
    @State var timeRuning = false
    @State var seconds = 0
    let defaults = UserDefaults.standard
    @State var timer = Timer()

    var body: some View {
        Group{
            HStack {
                Spacer()
                VStack(alignment: .center, spacing: 10.0) {
                    TopButtons(time: $seconds, timeRuning: $timeRuning)

                    Spacer()

                    TimeDisplay(time: $seconds)

                    Button(action: {
                        self.next()
                    }) {
                        Text(NSLocalizedString("NEXT", comment: ""))
                            .fontWeight(.semibold)
                            .font(.system(size: 60))
                            .color(.black)
                            .multilineTextAlignment(.center)
                            .padding(.all)
                        }.frame(width: 300.0, height: 300.0)
                        .background(Color.red).cornerRadius(50)

                    Spacer()
                }
                Spacer()
                }.background(self.setColor())
        }
    }

    func next() -> Void{
        if !self.timeRuning {
            runTimer()
        }
    }
    func runTimer() {
            self.seconds -= 1
    }
}

ConfigView

struct ConfigView : View {
    @Binding var timeInSeconds : String
    let defaults = UserDefaults.standard

    var body: some View {
        Group{
            HStack {
                Spacer()
                VStack {
                    Spacer()
                    Text("Tiempo Por Ronda")
                        .color(.white)
                        .font(.system(size: 40))
                        .frame(width: 200.0)
                    .multilineTextAlignment(.center)
                    .lineLimit(nil)
                TextField($timeInSeconds).frame(width: 100.0)    .textFieldStyle(.roundedBorder).background(Color.white)
                Spacer()
                    .frame(height: 50.0)
                    Button(action: {
                    self.guardar()
                    }) {
                        Text("Guardar").color(.white)
                            .cornerRadius(20)
                            .scaleEffect(3)
                            .animation(.basic(duration: 4))

                        }.frame(alignment: .center);
                    Spacer()



                }.frame(height: nil).accentColor(Color.orange)
                Spacer()

            }
    }.background(Color.black).edgesIgnoringSafeArea([.bottom,.top])

    }

    func guardar() -> Void {
        let time = Int(timeInSeconds) ?? 0
        defaults.set(time, forKey: "MAX_TIME")
    }
}

почему configView снова открывается? Предварительный просмотр

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