In swiftui
When I try to append from another screen to another screen
No exact matches in call to instance method 'append'
and
Value of type 'TextEditor' has no member 'text'
The alert appears.
Can't I use append?
Also, does the TextEditor not have text?
ContentView().array.append(textEditor.text)
says .
swiftui
No exact matches in call to instance method 'append'
I don't have any proof, but if you replace textEditor.text
in ContentView().array.append(textEditor.text)
with "Sample"
, or String
type literal, this message will probably disappear.There is no guarantee that the textEditor.text
type is the String
type, so the nuance is that the type does not match.However, even if the error persists, it has nothing to do with the nature of the problem.
Also, does the TextEditor not have text?
No, I don't have any.
Apple Developer Documentation Structure TextEditor
This page and the properties in the link View Implementations within this page are all the properties that the TextEditor
has.There are no properties anywhere else that are not in these two locations.
This Apple Developer Documentation comes with Xcode as well as Apple's site.If you have any questions, I think you'll get a lot better at your work and study if you first get into the habit of researching the Apple Developer Documentation.
●So how can a program receive the string you entered in TextEditor
?
The most important thing is that the idea doesn't fit in with the basic concept of SwiftUI.Avoid structures that involve GUI (Graphic User Interface) components at the core of the program.If you make any changes to the GUI configuration, the entire program should not be stopped.That's the basic concept of SwiftUI.
Write a simple program using TextEditor
.
import SwiftUI
struct ContentView:View {
@State var myText="
varbody:someView {
VStack {
TextEditor (text:$myText)
.padding()
Text (myText)
}
}
}
TextEditor
initiators must have String
type properties marked with @State
or @Binding
.You cannot create an instance without that argument.In the code above, the property myText
is substituted for the argument with $
.
In this way, the user's input to TextEditor
changes the value of the property myText
in real time.You can see that Text
that displays myText
changes with each type.
From the above, since the value of the string entered in TextEditor
is the same in real time as the property specified in the argument of the Initiator of TextEditor
, the property can be obtained by accessing it.You'll find out.
In addition, you can see that ContentView().array
access to the property array
is also contrary to the SwiftUI style, and ContentView
is only one part of the GUI.
(In addition, ContentView()
also raises the question of what you want to do by creating a new instance that is different from the existing ContentView
.)
Finally, if you enter any string on the modal screen, the list on the original screen will be displayed with the addition of the string you entered.
This sample code intentionally uses TextField
instead of TextEditor
.That's to give SwiftUI a hands-on experience of how GUI changes don't affect the fundamentals of the program. Replacing TextField
with TextEditor
would be interesting to try.
import SwiftUI
struct ContentView:View {
@State var myArray=["Sunday", "Monday", "Tuesday"]
@State var showModal=false
varbody:someView {
VStack {
List(myArray,id:\.self){weekName in
Text(weekName)
}
.padding()
Button("Add an Item") {
self.showModal.toggle()
}.sheet(isPresented:$showModal){
ModalView (isActive: $showModal, myArray: $myArray)
}
}
}
}
structureModalView:View {
@Binding var isActive:Bool
@Binding var myArray: [String]
@State private var newName=""
varbody:someView {
VStack {
Text("Input a new name.")
.padding()
TextField("New Name", text:$newName)
Button("Dismiss") {
myArray.append(newName)
isActive.toggle()
}
}
}
}
@available (iOS 15.0, *)
structureContentView_Previews:PreviewProvider{
static var previews:someView {
ContentView()
.previewInterfaceOrientation (.portrait)
}
}
© 2024 OneMinuteCode. All rights reserved.