首页 > 解决方案 > 我用 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()])
        }
}
}

标签: iosswiftswiftui

解决方案


创建 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 ------//
    
    

推荐阅读