SwiftUI 问题将变量传递给另一个视图 [英] SwiftUI Question passing a variable to another view

查看:27
本文介绍了SwiftUI 问题将变量传递给另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将浮动变量从一个视图传递到另一个新视图.

I am wanting to pass a Float Variable from one view to another new view.

在下面的代码中有一个名为 mhzValue 的 Float 值,它是通过 Slider 设置的,滑块更改了值,然后文本显示在视图中.. 当用户点击导航按钮以显示新的视图,我希望能够获取 mhzValue 并将其显示在文本框中的新视图中,并将其设置为另一个变量.

In the code below there is a Float value called mhzValue which is set by way of the Slider, the slider changes the value and Text is then displaying within the view.. When the user taps on the Navigation Button to display the new view, I would like to be able to take the mhzValue and display it in the new view in a text box, as well as set it as another variable.

希望这是有道理的.

请参阅下面的一些示例代码..

Please see some sample code below..

谢谢.

克雷格

import SwiftUI
struct ContentView : View {
    @State private var mhzValue : Float = 0
    var body: some View {
        // Navigation View
        NavigationView {

            VStack{
                Text("Test Value:")
                    .font(.headline)
                    .color(.blue)
                    .padding(.leading, -180.0)

                //Get Slider Value
                Slider(value: $mhzValue, from: 1, through: 55, by: 1)
                    .padding(.horizontal)
                //Display Slider Value
                Text("\(Int(mhzValue)) Value")
                    .font(.title)
                    .fontWeight(.semibold)
                    .color(.blue)
                // Naviagtion Button and send value of mhzValue to new View
                NavigationButton(destination: NextView()){
                    Image(systemName: "plus.square.fill")
                        .foregroundColor(.white)
                        .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
                        .frame(width: 150.0, height: 16.0)
                        .padding(15)
                        .background(Color.red)
                        .cornerRadius(10.0)
                }
            }
        }
    }
}

// New View to show Slider Value
struct NextView : View {
    var body: some View {
        Text("Display Slider Value Here:")

    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

推荐答案

这可以通过 绑定轻松完成.因为 mhzValue 是用 @State 属性包装器标记的,所以它有一个关联的 Binding.因此,您可以在第二 视图中声明一个 @Binding 变量,并使用与原始变量的 Binding 将其初始化.

This is easily done with Bindings. Because mhzValue is marked with the @State property wrapper, it has an associated Binding. You can therefore declare a @Binding variable in your second view, and initialize it with the Binding to the original variable.

struct NextView : View {
    @Binding var mhzValue: Float
    ...
}

当您指定 NextView 作为导航按钮的目标时,将其传递给 mhzValue 的绑定.(美元符号语法是引用绑定的一种简写方式.)

When you specify NextView as the destination for your navigation button, pass it a Binding to mhzValue. (The dollar-sign syntax is a shorthand way to refer to the binding.)

struct ContentView : View {
    @State private var mhzValue : Float = 0
    ...
    NavigationButton(destination: NextView(mhzValue: self.$mhzValue)){...}
    ...
}

然后您可以在 NextView 中使用 mhzValue:

You can then use mhzValue inside NextView:

struct NextView : View {

    @Binding var mhzValue: Float

    var body: some View {
        VStack{
            Text("Display Slider Value Here:")
            Text("\(Int(mhzValue)) Value")
                .font(.title)
                .fontWeight(.semibold)
                .color(.blue)
        }
    }
}

您在 NextView 中对 mhzValue 所做的任何更改实际上都是对 ContentView.mhzValue 的更改.

Any changes you make to mhzValue within NextView will effectively be changes to ContentView.mhzValue.

这篇关于SwiftUI 问题将变量传递给另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆