【问题标题】:on TabGesture Swift UI点击手势 Swiftui
【发布时间】:2019-10-27 02:27:45
【问题描述】:

在我研究 swiftUI 中的 scrollView 时寻求一些帮助。 我有一个显示数组值的滚动视图,基于当用户点击滚动视图的不同项目时,我想在下面的 textField 上显示它。

如何将数组值传递给文本字段??

import SwiftUI

struct ContentView: View {
    let post = ["TEST1 ","Test 2" , "Test 3","TEST4 ","Test 5" , "Test 6"]
    var temp = ""
    var body: some View {

        VStack {
            ScrollView(.horizontal, content: {
                        HStack(spacing: 100) {
                            ForEach(post, id: \.self){ item in


                                ZStack {
                                    Rectangle().foregroundColor(.blue).frame(width: 190, height: 170, alignment: .center)
                                    Text(item)
                                }.onTapGesture {
                                    // pass the value off Scroll View to the text
                                    debugPrint("\(item)")

                                }

                            }
                        }
                        .padding(.leading, 10)
                    })
                .frame(height: 190)
            Spacer()
            Text("dispaly here array value selected")
            Spacer()
        }

    }
}

谢谢你帮助我...

【问题讨论】:

    标签: arrays swift scrollview swiftui


    【解决方案1】:

    这里的窍门是当你需要在视图中分配一个@State 值时,你需要@State temp

    struct ContentView: View {
    let post = ["TEST1 ","Test 2" , "Test 3","TEST4 ","Test 5" , "Test 6"]
    
    @State private var temp = ""
    
    
    var body: some View {
    
        VStack {
            ScrollView(.horizontal, content: {
                        HStack(spacing: 100) {
                            ForEach(post, id: \.self){ item in
    
    
                                ZStack {
                                    Rectangle().foregroundColor(.blue).frame(width: 190, height: 170, alignment: .center)
                                    Text(item)
                                }.onTapGesture {
                                    // pass the value off Scroll View to the text
                                    self.temp = item
    
                                }
    
                            }
                        }
                        .padding(.leading, 10)
                    })
                .frame(height: 190)
            Spacer()
            Text( self.temp)
            Spacer()
           }
    
          }
       }
    

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多