Passing a map location variable to other view in SwiftUI - swiftui

I was able to get the location using CLLocationManager() and the city information is able to display in the Text(config.text). Can someone help how to pass that "config.text" to other view (mainView())
struct CityLocationView2 : View {
let location: Location
#State var config = CityLocationView2Config()
var body: some View {
Text(config.text)
mainView(). // How to pass the city ?????
.task(id: location.id) {
await config.reverseGecode(location: location)
}
}
}
Thanks,

try this:
struct CityLocationView2 : View {
let location: Location
#State var config = CityLocationView2Config()
var body: some View {
Text(config.text)
MainView(text: config.text) // <-- here
.task(id: location.id) {
await config.reverseGecode(location: location)
}
}
}
struct MainView: View {
#State var text: String // <-- here
var body: some View {
Text(text)
}
}
You can of course also use let text: String or var text: String, etc...
I recommend you read and do the tutorial at: https://developer.apple.com/tutorials/swiftui/
Knowing how to pass values to other Views is essential to use SwiftUI.

Related

SwiftUI - Binding in ObservableObject

Let's say we have a parent view like:
struct ParentView: View {
#State var text: String = ""
var body: some View {
ChildView(text: $text)
}
}
Child view like:
struct ChildView: View {
#ObservedObject var childViewModel: ChildViewModel
init(text: Binding<String>) {
self.childViewModel = ChildViewModel(text: text)
}
var body: some View {
...
}
}
And a view model for the child view:
class ChildViewModel: ObservableObject {
#Published var value = false
#Binding var text: String
init(text: Binding<String>) {
self._text = text
}
...
}
Making changes on the String binding inside the child's view model makes the ChildView re-draw causing the viewModel to recreate itself and hence reset the #Published parameter to its default value. What is the best way to handle this in your opinion?
Cheers!
The best way is to use a custom struct as a single source of truth, and pass a binding into child views, e.g.
struct ChildViewConfig {
var value = false
var text: String = ""
// mutating funcs for logic
mutating func reset() {
text = ""
}
}
struct ParentView: View {
#State var config = ChildViewConfig()
var body: some View {
ChildView(config: $config)
}
}
struct ChildView: View {
#Binding var config: ChildViewConfig
var body: some View {
TextField("Text", text: $config.text)
...
Button("Reset") {
config.reset()
}
}
}
"ViewConfig can maintain invariants on its properties and be tested independently. And because ViewConfig is a value type, any change to a property of ViewConfig, like its text, is visible as a change to ViewConfig itself." [Data Essentials in SwiftUI WWDC 2020].

How to initialize a #Binding Array

So I'm doing some refactoring and I ran across this line of code that I wanted to refactor:
struct MyView: View {
#State private var myArrayOfCustomObjects = [CustomObject]
let text: String
var body: some View {
Text(text)
}
}
Then when I wanted to refactor the view as so..
struct ExtractedView: View {
#Binding var customObjects: [CustomObject]
let text: String
init(customObjects: Binding<Array<CustomObject>>, text: String) {
self.customObjects = customObjects // Error: 'self' used before all stored properties are initialized
// Also tried _customObjects = customObjects
self.text = text
}
var body: some View {
Text(text)
}
}
This code is simplified of course but I fear I may be getting that error due to some complexity I'm not exposing in the example. Any feedback is welcome
What am I doing wrong??
( I also have an Environment instance (managedObjectContext) and a coreData class - which has some logic inside of the init that are being initialized too but didn't think it was relevant for this code example )
This will work! also try clean your build folder and build your project first.
struct ExtractedView: View {
#Binding var customObjects: [CustomObject]
let text: String
init(customObjects: Binding<Array<CustomObject>>, text: String) {
self._customObjects = customObjects
self.text = text
}
var body: some View {
Text(text)
}
}
struct CustomObject { }

How I can make my View-Extension update View with State input in SwiftUI?

Here I got an extension which works fine for updating itself at lunch, but after Lunch or Appear, it does not reports the change of data to its View.
My Goal: I want my Extension get sensitive to value that is reporting.
import SwiftUI
struct ContentView: View {
#State var changeString: String = "Hello, world!"
var body: some View {
customText().stringOfText(changeString).padding()
Button("change") { changeString = "Omid" }.padding()
Text(changeString).padding()
}
}
struct customText: View {
#State var stringOfText = ""
var body: some View
{
Text(stringOfText)
}
}
extension customText {
func stringOfText(_ text: String) -> customText {
customText(stringOfText: text)
}
}
It should not be state inside, because state is not recreated, ie. you need
struct customText: View {
var stringOfText: String = "" // << here !!
var body: some View {
Text(stringOfText)
}
}

How we can read #State variable from other struct in SwiftUI?

I want to know if there is simple or proper way to read a State variable value from a different View, I know the usage of .onChange or Binding or ObservableObject(class) and ..., but I like to know is there any other better way?
For example in this code I have a View called TextView which has a State value, and I am calling this View inside my ContentView, Now I put a Text in my ContentView which I want to read the State Value of TextView. Is there a spacial method for this job?
struct ContentView: View {
#State var readStringOfTextView: String = ""
var body: some View {
TextView()
Text(readStringOfTextView)
.foregroundColor(Color.blue)
}
}
struct TextView: View {
#State var stringOfText: String = "Hello, world!"
var body: some View {
Text(stringOfText)
.padding()
.foregroundColor(Color.red)
}
}
The entire point of State is that it's internal to a View. If you're trying to read it elsewhere, something has gone wrong in your design. The tool you want in this case is #Binding. ContentView should pass a Binding to TextView. Any changes in TextView will be seen by ContentView (in your example, this doesn't make sense, because stringOfText can't change, but I assume that the rest of your code changes it somehow). In your example, that would look something like this:
struct ContentView: View {
#State var readStringOfTextView: String = ""
var body: some View {
TextView(stringOfText: $readStringOfTextView)
Text(readStringOfTextView)
.foregroundColor(Color.blue)
}
}
struct TextView: View {
#Binding var stringOfText : String
var body: some View {
Text(stringOfText)
.padding()
.foregroundColor(Color.red)
.onAppear {
stringOfText = "Hello, world!"
}
}
}
In is possible to directly pass data up the view hierarchy using Preferences, but it's much more complicated, and not the right tool for the problem you've described. Even so, this is what it would look like:
Create a PreferenceKey to pass the data
Set the PreferenceKey in the child view(s) using .preference
Read the PreferenceKey in the parent view using .onPreferenceChange or .overlayPreferenceValue or .backgroundPreferenceValue.
struct TextPreference: PreferenceKey {
static var defaultValue = "default"
static func reduce(value: inout String, nextValue: () -> String) {
value = nextValue()
}
}
struct ContentView: View {
#State var readStringOfTextView: String = ""
var body: some View {
VStack {
TextView()
.onPreferenceChange(TextPreference.self) { value in
readStringOfTextView = value
}
Text(readStringOfTextView)
.foregroundColor(Color.blue)
}
}
}
struct TextView: View {
#State var stringOfText : String = "Hello, world!"
var body: some View {
Text(stringOfText)
.padding()
.foregroundColor(Color.red)
.preference(key: TextPreference.self, value: stringOfText)
}
}

Use protocol to define property of swiftui view

I have multiple classes that I want to use with a budget picker view. They all have this budgetable protocol defined.
import SwiftUI
struct BudgetPickerView: View {
#EnvironmentObject var userData: UserData
#State var budgetable: Budgetable
...
}
import Foundation
protocol Budgetable
{
var budgetId: String { get set }
}
For example this Allocation class
import Foundation
import Combine
class Allocation: ObservableObject, Identifiable, Budgetable {
let objectWillChange = ObservableObjectPublisher()
let id: String?
var amount: String { willSet { self.objectWillChange.send() } }
var budgetId: String { willSet { self.objectWillChange.send() } }
init(id: String? = nil, amount: String, budgetId: String) {
self.id = id
self.amount = amount.removePrefix("-")
self.budgetId = budgetId
}
}
However, when I try to pass an allocation into my budget picker view I get an error
NavigationLink(destination: BudgetPickerView(budgetable: allocation))...
Cannot convert return expression of type 'NavigationLink>, BudgetPickerView>' to return type 'some View'
Expression type 'BudgetPickerView' is ambiguous without more context
Change as bellow code
struct BudgetPickerView: View {
#EnvironmentObject var userData: UserData
var budgetable: Budgetable
var body: some View {
...
}
}
and
NavigationLink(destination: BudgetPickerView(budgetable: allocation).EnvironmentObject(UserData()))
By SwiftUI concept you are not allowed to work with #State outside of View, but the following works well (having other your parts unchanged)
struct BudgetPickerView: View {
#State private var budgetable: Budgetable
init(budgetable: Budgetable) {
_budgetable = State<Budgetable>(initialValue: budgetable)
}
var body: some View {
Text("Hello, World!")
}
}
struct TestBudgetPickerView: View {
var body: some View {
NavigationView {
NavigationLink(destination:
BudgetPickerView(budgetable: Allocation(amount: "10", budgetId: "1")))
{ Text("Item") }
}
}
}
BTW, just incase, again by design #State is intended to hold temporary-view-state-only data, not a model. For model is more preferable to use ObservableObject. In your case Budgetable looks like a model.