英文:
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 }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论