I would like to display a Text (view) in SwiftUI, that has mutable String content.
@State var myvar : String = "Undefined"
var body: some view {
Text("Content: \(myvar)")
}
func abc() {
self.myvar = "Defined"
}
Whenever I call the abc() function, the value of myvar doesn't get updated. I have checked this using a debugPrint(self.myvar) in abc().
This code is inside my ContentView.swift, while I am calling contentView.abc() from HostingController.swift in a delegate callback method.
Is there a solution to create mutable/changeable Strings for this "external" use in SwiftUI?
EDIT:
From HostingController:
override var body: ContentView {
let contentView = ContentView().environmentObject(contentModel)
...
return contentView
The last line gives the error ´Cannot convert return expression of type some View to return type ContentView´. It is to note, that I am working in a WatchKit context, so this is an Apple Watch Application.
abc()? (I feel the reason is in it) - Asperi@Stateonly within View itself. - Asperi