Reputation: 73
I am creating a text control which accepts optional text value. If the value is provided I would like to show TextField control otherwise use Text Control. Can you please guide me how can I rebind already binded value to a text field
struct TextBoxControl: View {
var text : String
@Binding var value : String?
var body: some View {
if (value == nil )
{
Text(text)
}
else
{
TextField("Enter value", text: $value!)
}
}
}
Upvotes: 1
Views: 52
Reputation: 257693
It is much simpler for your case, just
TextField("Enter value", text: Binding($value)!)
Upvotes: 0
Reputation: 73
Great I found a solution
//'''
struct TextBoxControl: View {
var text : String
//@Binding var value : String?
var value : Binding<String>?
@State var dummyText : String = ""
var body: some View {
if (value == nil )
{
Text(text)
}
else
{
TextField("Enter value", text: (value!) ?? $dummyText)
}
}
}
struct TextBoxControlTest: View {
var text : String
@State var txt : String
//var value : Binding<String>?
@State var dummyText : String = ""
var body: some View {
TextBoxControl(text: "ddd", value: ($txt))
}
}
//'''
Upvotes: 1