在SwiftUI中了解@Binding和PresentationView

时间:2019-06-11 21:15:07

标签: swift swiftui

我有2个视图(主视图和配置视图)

具有简单倒计时的mainView 和configView带有最大倒计时值...

在主视图上,有一个PresentationButton可以转到ConfigView

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

如果我在configview上设置了最大值,然后在主视图上更改了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再次打开? preview

0 个答案:

没有答案