gpt4 book ai didi

ios - Swift UI : Disable a button if a text field is empty

转载 作者:行者123 更新时间:2023-12-01 19:35:35 24 4
gpt4 key购买 nike

如果 Swift UI 的文本字段为空,我如何禁用按钮。我的代码:

struct AddTask: View {
@Binding var isOpen: Bool
@State var text = ""
let tint: Color
let done: (String) -> ()

var body: some View {
VStack() {
HStack {
Text("What tasks are you planning to do?")
.font(.custom("Avenir", size: 14))
.foregroundColor(.gray)
Spacer()
}

CustomTextField(text: $text, isFirstResponder: true)
.frame(height: 30)

HStack {
Spacer()
Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
.foregroundColor(self.tint)
}
Spacer()
}
.padding(.all, 30)
}
}

如果 CustomTextField 为空,我想禁用按钮。

最佳答案

使用 .disabled修饰符如下

    Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
.foregroundColor(self.tint)
.disabled(self.text.isEmpty)

关于ios - Swift UI : Disable a button if a text field is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com