Home>
Hello.
We would appreciate your help in using UITextField in SwiftUI.
I was able to describe up to displaying UITextField using UIViewRepresentable, but the initial value of the binding variable is not reflected in View, which is a problem.
How can I display "sample" in TextField when the View is displayed from the following source code?
Thanks for your professor.
struct ContentView: View {
@State var text = "sample"
var body: some View {
MyTextField("", text: $text)
}
}
class MyTextFieldCoordinator: NSObject {
var control: MyTextField
init(_ control: MyTextField) {
self.control = control
super.init()
control.textField.addTarget(self, action: #selector(textFieldEditingDidBegin(_:)), for: .editingDidBegin)
control.textField.addTarget(self, action: #selector(textFieldEditingDidEnd(_:)), for: .editingDidEnd)
control.textField.addTarget(self, action: #selector(textFieldEditingChanged(_:)), for: .editingChanged)
control.textField.addTarget(self, action: #selector(textFieldEditingDidEndOnExit(_:)), for: .editingDidEndOnExit)
}
@objc private func textFieldEditingDidBegin(_ textField: UITextField) {
control.onEditingChanged(true)
}
@objc private func textFieldEditingDidEnd(_ textField: UITextField) {
control.onEditingChanged(false)
}
@objc private func textFieldEditingChanged(_ textField: UITextField) {
control.text = textField.text ?? ""
}
@objc private func textFieldEditingDidEndOnExit(_ textField: UITextField) {
control.onCommit()
}
}
struct MyTextField: UIViewRepresentable {
private let title: String?
@Binding var text: String
let onEditingChanged: (Bool) ->Void
let onCommit: () ->Void
let textField = UITextField()
init(_ title: String?, text: Binding<String>, onEditingChanged: @escaping (Bool) ->Void = {_ in }, onCommit: @escaping () ->Void = {}) {
self.title = title
self._text = text
self.onEditingChanged = onEditingChanged
self.onCommit = onCommit
}
func makeCoordinator() ->MyTextFieldCoordinator {
MyTextFieldCoordinator(self)
}
func makeUIView(context: Context) ->UITextField {
textField.placeholder = title
textField.delegate = context.coordinator as? UITextFieldDelegate
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
}
}
Supplemental information (FW/tool version, etc.)
Xcode version is 11.6.
-
Answer # 1
Related articles
- javascript - i want to receive and display a value in a dialog box
- ios - i'm stuck with a basic tableview display
- javascript - about initial value setting of constructor using objectassign
- tomcat - i want to display the value submitted from the form, but i get http status 404-not found
- vuejs - how to set the initial value of v-text-field
- php - i want to display the average value of db records passed from laravel to vue
- ios - i want to change the bool value of uiswitch
- [swift5, ios-charts] i want to display a circle (round spot) in a specific place (value)
- ios - cannot pass by value with navigation controller
- php - i want to display the value of a custom field as a column in the wordpress custom post list
- ios - i want to display an alert [swift]
- [swift] i want to read the initial data (seed file) of realm, but i can't do it on ios 11
- ruby - when editing the initial value of text_field
- ios - swift value of type'cameraviewcontroller' has no member'pinchedgesture' error
- c ++ - i want to set an initial value for a static member variable
- swift - initial value of uimenuelementattributes? about the value to set
- i want to display the array value of another function in the main function
- javascript - i want to automatically calculate and display the value entered in the input form with javasprict
- display of maximum value and minimum value of java
Related questions
- ios - i want to pass an array containing tuples in swift and userdefaults
- ios - it is about line breaks when using uitextview wrapped in swift ui
- ios - [swift] i want to display the image dialog
- ios - the terminal screen remains blank for a while when debugging the actual device
- ios - core bluetooth data exchange
- ios - transition screen in authentication with firebase
- whether or not a small screen like picture-in-picture can be realized on the screen of a self-made ios application
- ios - [swift] how to get all the values in the tableview cell and pass them to the transition destination
- ios - i wanted to implement a semi-modal view like google map
- ios - i want to set a newly created storyboard in xcode as the main interface
solved.
I was able to reflect the initial value by adding the following line to init of MyTextField.