ios - 我用 forEach 创建了一个 Circle,但我不知道如何检查其中的数据。/SwiftUI
问题描述
我用 ForEach 创建了一个 Circle,但我不知道如何检查其中的数据。如果圆圈内没有照片,我希望它发出警告。我不知道怎么做。你能解决这个问题吗?我想在整个圈子满后采取行动。SwiftUi 正在学习新东西
struct SendPi: View {
var body: some View {
GeometryReader { geometry in
Color.black.edgesIgnoringSafeArea(.all)
VStack {
HStack(spacing: 30) {
ForEach(0 ..< 3) { _ in
CircleView()
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.shadow(color: Color.yellow, radius: 10, x: 0, y: 0)
}
}
Spacer().frame(height: geometry.size.height / 15)
VStack {
Button(action: {}, label: {
Text("Gönder")
.frame(width: geometry.size.width / 3, height: 40)
.padding(10)
.font(Font.system(size: 30, weight: .medium, design: .serif))
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 45))
.foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))
})
}
}
}
}
}
struct CircleView: View {
@State private var image: Image?
@State private var shouldPresentImagePicker = false
@State private var shouldPresentActionScheet = false
@State private var shouldPresentCamera = false
var imageView: Image {
image ?? Image("plus")
}
var body: some View {
imageView
.resizable()
.aspectRatio(contentMode: .fill)
.clipShape(Circle())
.overlay(Circle().stroke(Color.yellow, lineWidth: 4))
.onTapGesture { self.shouldPresentActionScheet = true }
.sheet(isPresented: $shouldPresentImagePicker) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker)
}
.actionSheet(isPresented: $shouldPresentActionScheet) { () -> ActionSheet in
ActionSheet(title: Text("Choose mode"), message: Text("Please choose your preferred mode to set your profile image"), buttons: [ActionSheet.Button.default(Text("Camera"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Photo Library"), action:
{
self.shouldPresentImagePicker = true
self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
}
}
解决方案
创建 DataModel 和一组数据模型。这样,您可以跟踪您的数据。
例子:
数据模型
struct ImageModel: Identifiable {
var id: Int
var image: Image?
}
你的看法
struct ContentView: View {
@State private var arrImages: [ImageModel] = (0..<3).map { (index) -> ImageModel in ImageModel(id: index) }
var body: some View {
GeometryReader { geometry in
Color.black.edgesIgnoringSafeArea(.all)
VStack {
HStack(spacing: 30) {
ForEach(arrImages.indices) { index in
CircleView(image: $arrImages[index].image)
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.shadow(color: Color.yellow, radius: 10, x: 0, y: 0)
}
}
Spacer().frame(height: geometry.size.height / 15)
Button {
checkData()
} label: {
Text("Check")
}
}
}
}
// Check your data and do anything with your data array.
private func checkData() {
for data in arrImages {
if data.image == nil {
print("Image is not available at index ", data.id)
} else {
print("Image is available at index ", data.id)
}
}
}
}
在 CircleView 中,绑定您的图像。
struct CircleView: View {
@Binding var image: Image?
//---- Other Code ------//
推荐阅读
- powerbi - 如何使用 Power Automate 将包含数百万行的表中的所有数据从 Power BI 导出到 csv?
- javascript - 如何在javascript中划分多个数组
- ansible - ansible playbook 用于检查目标主机的服务器可达性
- reactjs - React - 将道具传递给特定的孩子
- julia - 在 Julia 中的同一图形上绘制多个 ODE 解决方案的最佳方法是什么?
- javascript - 如何将上传的图片插入位于另一张图片上的框架中?
- clang - 为 WebAssembly 创建程序集数据部分
- php - imagick 只保存 gif 的第一帧
- java - Android Studio Firestore RecyclerView
- codeigniter - 如何在 Codeigniter 4 的同一行中编写方法和分页