限制列表的对象数量 - SwiftUi

Limit List's objects amount - SwiftUi

我的 WatchOS 应用 运行 在 Core Data 上。那里有 2 个视图:

  1. FirstView - 带有用于添加新目标和已添加目标列表的按钮的视图。
  2. AddGoalView - 在按下添加新目标后出现。

我还添加了 List,因为我想在我的应用程序中使用 .onDelete

我试图限制用户创建无限数量的目标。我想提供最多添加 10 个目标的机会,然后 AddGoal 按钮将被禁用。你有什么建议吗?

首先查看代码:

struct FirstView: View {
@Environment(\.managedObjectContext) var context
    @FetchRequest (
    entity:NewGoal.entity(),
       sortDescriptors:[NSSortDescriptor(keyPath: \NewGoal.dateAdded, ascending: false)],
        animation: .easeInOut )

var results:FetchedResults<NewGoal>
var body: some View {
        VStack{
        VStack(alignment: .leading){
            NavigationLink(
                    destination: AddGoalView(),
                    label: {
                        Text("Set Money Goal")})
                        Text("10/10")

             VStack(alignment: .leading){
                List{
                ForEach(results){ item in
                    NavigationLink(
                        destination: TABmodule(goalItem: item, GTitle: item.goalTitle ?? "", Sum: item.neededSum, summarize: item.yourSum),
                            label: {
                                HStack{
                                        HStack(alignment: .center, spacing:0){
                                            Text(String(item.indDatenum))
                                            Text(item.pickedValueDateS ?? "")
                                        }

                                VStack(alignment: .leading){
                                    Text(item.goalTitle ?? "")
          
                                    HStack(spacing: 0){
                                        Text("$\(item.yourSum, specifier: "%.f")")
                                        Text("/ $\(item.neededSum, specifier: "%.f")")
                                    }
                                }
                                }
                    }).clipShape(RoundedRectangle(cornerRadius: 9))
                    
                } .onDelete(perform: deleteGoal2)
                }.listStyle(PlainListStyle())

                }
            }
        }
    }
func deleteGoal2(at offsets: IndexSet) {
    for index in offsets {
        let item = results[index]
        context.delete(item)
    }
    do{
        try context.save()
    }catch let err{
            print(err.localizedDescription)
            }
}
}

AddGoalView 上的 AddGoal 函数:

    private func addGoal(){
    
    if index == 0 {
        pickedValueDateS = "W"
        pickedValueDateN = 7 * indexnum
        pickedValueDateN1 = 7 * indexnum
        } else if index == 1{
            pickedValueDateS = "M"
            pickedValueDateN = 30 * indexnum
            pickedValueDateN1 = 30 * indexnum
        } else if index == 2{
            pickedValueDateS = "Y"
            pickedValueDateN = 365 * indexnum
            pickedValueDateN1 = 365 * indexnum
    } else {
        pickedValueDateS = "N/A"
        pickedValueDateN = 1
    }
    let goal = NewGoal(context: context)
    goal.goalTitle = goalTitle
    goal.dateAdded = Date()
    goal.neededSum = neededSum
    goal.pickedValueDateS = pickedValueDateS
    goal.allNewSum = allNewSum
    goal.pickedValueDateN = Int64(pickedValueDateN)////ALL DAYS
    goal.pickedValueDateN1 = Int64(pickedValueDateN1)////MINUS DAYS
    goal.dayChange = Int64(daychange)
    goal.indDate = Int64(index)
    goal.indDatenum = Int64(indexnum)
    
    
    do{
        try context.save()
        presentationMode.wrappedValue.dismiss()
    }catch let err{
        print(err.localizedDescription)
        }
    }

将此添加到您想要禁用的任何 View,例如 NavigationLink,这样用户就无法转到 View

.disabled(results.count >= 10)