SwiftUI-通过按钮传递动态数据

时间:2019-08-26 17:44:48

标签: state swiftui bindable

我正在尝试通过Button动作将动态详细信息从母版页面传递到详细信息页面。我正在传递的视图(从屏幕外进行动画处理)是使用动画进入的视图。当我传递简单的硬编码数据时,我能够使它正常工作,但是在尝试弄清楚如何使其传递所点击的项目行特定的数据时遇到了一个问题。

我已经用按钮包裹了项目行元素,但是不确定我是否将正确的信息传递给了按钮动作参数。

import SwiftUI

struct SearchView: View {
@State var show = false
var course = courseData
var body: some View {

    ZStack {

        ScrollView (.vertical, showsIndicators: false) {

            VStack(spacing: 0.0) {
                ForEach(course) { item in
                    Button(action: { self.show.toggle() })  {
                        CourseResultTile(perks: item.perks, hotdeals: item.hotdeals, image: item.image, courseName: item.courseName, distance: item.distance, reviews: item.reviews, priceSpan: item.priceSpan, timeSpan: item.timeSpan)
                    }
                }
            }
        }.offset(y: 120)

//视图在下面显示动画//

        FacilityView(show: $show, perks: self.perks, image: self.image, courseName: self.courseName, address: self.address)
            .rotation3DEffect(Angle(degrees: show ? 0 : 60), axis: (x: 0, y: 10.0, z: 0))
            .animation(.default)
            .offset(x: show ? 0 : -UIScreen.main.bounds.width)
            .onTapGesture {
                self.show.toggle()
        }

    }
    .navigationBarTitle(Text("Search"), displayMode: .inline)
    .navigationBarHidden(true)
    .edgesIgnoringSafeArea(.top)



}
}

//课程数据模型//

struct Course : Identifiable {
var id = UUID()
var hotdeals: String
var perks: String
var image: String
var courseName: String
var distance: String
var reviews: String
var priceSpan: String
var timeSpan: String
var address: String
}

let courseData = [
Course(hotdeals: "Badge_HotDeals", perks: "Badge_NoPerks", image: "Image_CourseBackground", courseName: "Timacuan Golf Club", distance: "16.8 miles", reviews: "(562)", priceSpan: "$12.00 - $34.00", timeSpan: "1:53PM - 6:00 PM", address: "550 Timacuan Boulevard, Lake Mary, Florida, 32746"),
Course(hotdeals: "Badge_HotDeals", perks: "Badge_NoPerks", image: "Image_Course1", courseName: "Sanctuary Ridge Golf Club", distance: "20.1 miles", reviews: "(895)", priceSpan: "$12.00 - $34.95", timeSpan: "8:46 AM - 5:02 PM", address: "2601 Diamond Club Drive, Clermont, Florida, 34711"),
Course(hotdeals: "Badge_HotDeals", perks: "Badge_NoPerks", image: "Image_Course2", courseName: "Eagle Creek Golf Club - FL", distance: "14.3 miles", reviews: "(1K)", priceSpan: "$12.00 - $42.00", timeSpan: "8:42 AM - 5:51 PM", address: "10350 Emerson Lake Blvd, Orlando, Florida, 32832")

// FacilityView摘录//

import SwiftUI

struct FacilityView: View {

var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}


var perks = "Badge_NoPerks"
var image = "Image_Course6"
var courseName = "Panther Lake: Orange County National"
var address = "16301 Phil Ritson Way, Winter Garden, FL 34787"
var holes = "18"
var par = "72"
var length = "6836"
var slope = "127"

@Binding var show: Bool
@State private var todaysDate = Date()

我得到的错误是“ SearchView类型的值没有成员我要传递的变量”

0 个答案:

没有答案