在Swift/SwiftUI中在ForEach循环中使用弹出视图?

huangapple go评论71阅读模式
英文:

Use a popover in a ForEach loop (Swift/SwiftUI)?

问题

I'm working on an app in which there are several buttons in a VStack, and each of them should have their own popover when clicked on. However, when I click on each button, the popover does not appear.

The code I've tried is below. I should mention that I'm very new to Swift/SwiftUI. Again, when I click on each button (which is a ZStack), it seems like the click is registered but nothing appears. If anyone could help, that would be great! Thanks so much!

struct TodayView: View {
    @State var showingPopover: Bool
    
    init() {
        getAccess()
        courses = getEvents(name: "Home") //name should be "All Classes"
        showingPopover = false
    }
    
    var body: some View {
        NavigationView {
            ZStack {
                background
                ScrollView {
                    VStack {
                        ForEach(courses, id: \.self) { course in
                            Button {
                                showingPopover = true
                            } label: {
                                ZStack {
                                    Image("event_img")
                                        .resizable()
                                        .scaledToFit()
                                    Text(course.title)
                                        .foregroundColor(.white)
                                        .fontWeight(.heavy)
                                        .font(.system(size: 28))
                                }
                            }
                            .popover(isPresented: $showingPopover) {
                               Text("Hello")
                            }
                        }
                    }
                }
            }
            .navigationTitle("Today")
        }
    }
}
英文:

I'm working on an app in which there are several buttons in a VStack, and each of them should have their own popover when clicked on. However, when I click on each button, the popover does not appear.

The code I've tried is below. I should mention that I'm very new to Swift/SwiftUI. Again, when I click on each button (which is a ZStack), it seems like the click is registered but nothing appears. If anyone could help, that would be great! Thanks so much!

struct TodayView: View {
    @State var showingPopover: Bool
    
    init() {
        getAccess()
        courses = getEvents(name: "Home") //name should be "All Classes"
        showingPopover = false
    }
    
    var body: some View {
        NavigationView {
            ZStack {
                background
                ScrollView {
                    VStack {
                        ForEach(courses, id: \.self) { course in
                            Button {
                                showingPopover = true
                            } label: {
                                ZStack {
                                    Image("event_img")
                                        .resizable()
                                        .scaledToFit()
                                    Text(course.title)
                                        .foregroundColor(.white)
                                        .fontWeight(.heavy)
                                        .font(.system(size: 28))
                                }
                            }
                            .popover(isPresented: $showingPopover) {
                               Text("Hello")
                            }
                        }
                    }
                }
            }
            .navigationTitle("Today")
        }
    }
}

答案1

得分: 0

以下是您要翻译的内容:

尝试使用这种方法为所选的“课程”显示一个弹出窗口。在ForEach循环之外使用.popover(item: $selectedCourse)

示例代码假定您的数组courses包含类型为Course的元素。

struct TodayView: View {
    // for testing
    @State var courses = ....
    @State var selectedCourse: Course?  // <-- 这里
    
    init() {
         getAccess()
         courses = getEvents(name: "Home") //name should be "All Classes"
    }
    
    var body: some View {
        NavigationView {
            ZStack {
                背景
                ScrollView {
                    VStack {
                        ForEach(courses, id: \.self) { course in
                            Button {
                                selectedCourse = course // <-- 这里
                            } label: {
                                ZStack {
                                    Image("event_img")
                                        .resizable()
                                        .scaledToFit()
                                    Text(course.title)
                                        .foregroundColor(.white)
                                        .fontWeight(.heavy)
                                        .font(.system(size: 28))
                                }
                            }
                        }
                    }
                }
            }
            .popover(item: $selectedCourse) { course in  // <-- 这里
                Text(course.title)
            }
            .navigationTitle("今天")
        }
    }
}

EDIT-1:

要将`EKEvent`用作课程,

添加以下内容以使`EKEvent`可识别:

```swift
extension EKEvent: Identifiable {
    public var id: String { self.eventIdentifier }
}
英文:

Try this approach to display a popover for the desired selected Course. Using a .popover(item: $selectedCourse) outside the ForEach loop.

The example code assumes that your array courses consists of type Course elements.

struct TodayView: View {
    // for testing
    @State var courses = ....
    @State var selectedCourse: Course?  // <-- here
    
    init() {
         getAccess()
         courses = getEvents(name: "Home") //name should be "All Classes"
    }
    
    var body: some View {
        NavigationView {
            ZStack {
                background
                ScrollView {
                    VStack {
                        ForEach(courses, id: \.self) { course in
                            Button {
                                selectedCourse = course // <-- here
                            } label: {
                                ZStack {
                                    Image("event_img")
                                        .resizable()
                                        .scaledToFit()
                                    Text(course.title)
                                        .foregroundColor(.white)
                                        .fontWeight(.heavy)
                                        .font(.system(size: 28))
                                }
                            }
                        }
                    }
                }
            }
            .popover(item: $selectedCourse) { course in  // <-- here
                Text(course.title)
            }
            .navigationTitle("Today")
        }
    }
}

EDIT-1:

to use EKEvent as courses,

add this to make EKEvent Identifiable:

extension EKEvent: Identifiable {
    public var id: String { self.eventIdentifier }
}

huangapple
  • 本文由 发表于 2023年6月8日 06:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定