首页 > 解决方案 > 如何取消 TextField 编辑

问题描述

如何取消文本字段编辑。TextField我不希望在进行选择时进行其他编辑。我自己试过了,但我做不到。

struct profile: View {
    @State private var iliskiDurumlari: [String] = ["Evli", "Bekar", "Ayrı", "anan"]
    @State private var iliskiVisible = false
    @State private var iliski = ""

    var body: some View {
        VStack {
            TextField("İlişki Durumu Seçiniz..", text: $iliski)
                .frame(width: 300, height: 50, alignment: .center)
                .padding(5)
                .font(Font.system(size: 15, weight: .medium, design: .serif))
                .overlay(
                    RoundedRectangle(cornerRadius: 30)
                        .stroke(Color(red: 45 / 255, green: 0 / 255, blue: 112 / 255), lineWidth: 1)
                )
                .actionSheet(isPresented: $iliskiVisible, content: actionSheet)
                .onTapGesture {
                    self.iliskiVisible.toggle()
                }
        }
    }
    
    func actionSheet() -> ActionSheet {
        ActionSheet(
            title: Text("Bir İlişki Durumu Seçiniz"),
            message: Text("Aşagıda"),
            buttons: iliskiDurumlari.map { value in
                ActionSheet.Button.default(Text(value), action: {
                    self.iliski = value
                })
            }
        )
    }
}

标签: iosswiftswiftui

解决方案


如果您不需要此特定字段的用户输入(显示键盘),则可以Text改用:

var body: some View {
    VStack {
        Text(iliski) // <- change here
            .frame(width: 300, height: 50, alignment: .center)
            .padding(5)
            .font(Font.system(size: 15, weight: .medium, design: .serif))
            .overlay(
                RoundedRectangle(cornerRadius: 30)
                    .stroke(Color(red: 45 / 255, green: 0 / 255, blue: 112 / 255), lineWidth: 1)
            )
            .actionSheet(isPresented: $iliskiVisible, content: actionSheet)
            .onTapGesture {
                self.iliskiVisible.toggle()
            }
    }
}

而不是占位符添加初始值:

@State private var iliski = "İlişki Durumu Seçiniz.."

推荐阅读