swift 加号按钮不显示用户写入的文本

qyzbxkaa  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(128)

这是我现在的代码。我试过无数种方法,但没有一种有效。
在点击加号按钮并写入文本并点击“添加”后,它应该显示已经写入的内容,但没有显示任何内容。

struct LandingView: View {
    @State private var newLandingName: String = ""
    @State private var isAddingLanding = false
    @State private var landings: [String] = []
    
    var body: some View {
        NavigationView {
            ZStack {
                Color.blue.ignoresSafeArea()
                Circle()
                    .scale(1.7)
                    .foregroundColor(.yellow.opacity(0.60))
                Circle()
                    .scale(1.3)
                    .foregroundColor(.blue.opacity(0.50))
                
                VStack {
                    HStack {
                        Text("Add Landing!")
                            .bold()
                            .font(.largeTitle)
                            .padding()
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .alignmentGuide(.leading) { _ in 0 }
                            .frame(maxHeight: .infinity, alignment: .top)
                            .alignmentGuide(.top) { _ in 0 }
                        Spacer()
                        ZStack {
                            GeometryReader { geometry in
                                Button(action: {
                                    isAddingLanding = true
                                }) {
                                    Image(systemName: "plus")
                                        .resizable()
                                        .frame(width: 20, height: 20)
                                        .foregroundColor(.white)
                                }
                                .frame(width: 40, height: 40)
                                .position(x: geometry.size.width - 30, y: 30)
                            }
                        }
                        .padding(.trailing, 10)
                    }
                    LandingNameView(newLandingName: newLandingName)
                }
                .sheet(isPresented: $isAddingLanding, onDismiss: {
                    if !newLandingName.isEmpty {
                        landings.append(newLandingName)
                    }
                    newLandingName = ""
                }) {
                    VStack {
                        TextField("Enter landing name: \(newLandingName)", text: $newLandingName)
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                            .padding()
                        
                        Button("Add") {
                            if !newLandingName.isEmpty {
                                landings.append(newLandingName)
                                newLandingName = ""
                            }
                            isAddingLanding = false
                        }
                        .padding()
                        .frame(maxWidth: .infinity)
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                        .padding()
                    }
                }
            }
            List(landings, id: \.self) { landing in
                
            }
            .navigationBarTitle(Text("Landings"))
        }
    }
}

试图让文本显示一旦用户写它。

6tdlim6h

6tdlim6h1#

首先,重新组织视图层次结构。
这是我现在的代码。我试过无数种方法,但没有一种有效。在点击加号按钮,写文本,并点击“添加”后,它应该显示已经写了什么,但什么也没有显示。
它没有显示,因为下面的元素中没有文本

List(landings, id: \.self) { landing in }
// Change it to
List(landings, id: \.self) { landing in Text(landing) }

并且此元素不在正确的视图层次结构中。像这样放。

struct LandingView: View {
    @State private var newLandingName: String = ""
    @State private var isAddingLanding = false
    @State private var landings: [String] = []
    var body: some View {
        NavigationView {
            ZStack {
                Color.blue.ignoresSafeArea()
                Circle().scale(1.7).foregroundColor(.yellow.opacity(0.60))
                Circle().scale(1.3).foregroundColor(.blue.opacity(0.50))
                VStack {
                    HStack {
                        Text("Add Landing!")
                            .alignmentGuide(.leading) { _ in 0 }
                            .frame(maxHeight: .infinity, alignment: .top)
                            .alignmentGuide(.top) { _ in 0 }
                        Spacer()
                        ZStack {
                            GeometryReader { geometry in
                                Button(action: { isAddingLanding = true }) {
                                    Image(systemName: "plus").frame(width: 20, height: 20).foregroundColor(.white)
                                }
                                .frame(width: 40, height: 40)
                                .position(x: geometry.size.width - 30, y: 30)
                            }
                        }.padding(.trailing, 10)
                    }
                    List(landings, id: \.self) { landing in Text(landing) }.navigationBarTitle(Text("Landings"))
                }
                .sheet(isPresented: $isAddingLanding, onDismiss: {}) {
                    VStack {
                        TextField("Enter landing name: \(newLandingName)", text: $newLandingName)
                        Button("Add") {
                            if !newLandingName.isEmpty {
                                landings.append(newLandingName)
                                newLandingName = ""
                            }
                            isAddingLanding = false
                        }
                        .cornerRadius(10)
                        .padding()
                    }
                }
            }
        }
    }
}

相关问题