0

I have this code:

Main view

import SwiftUI

struct ContentView: View {
    
   @EnvironmentObject var data:Pessoa
var body: some View {
    NavigationView{
        
        VStack{
            
            NavigationLink(destination:view2(data: data)){
                Text(data.data.firstObject as! String)
            }
        }
    }.environmentObject(data)
}
}

2nd view

import SwiftUI

struct view2: View {
    
  var data:Pessoa
    
var body: some View {
    
    Button(action: {
        
        self.data.data[0] = "New btn Text"
    }){
        Text("Edit Btn Texr")
        
    }.environmentObject(data)
    
}
}

Class Pessoa

class Pessoa:ObservableObject {
    var data:NSMutableArray
     init() {
        self.data = NSMutableArray()
        self.data.add("Btn")
    }
}

How I can update the main view when I return form the 2nd view.

Yes I need to pass the object or at least, the array.

The main idea is in a structure like:

V1 -> v2 -> V3

if I make a change in some parameter of a class, in the V3, how I can propagate (in the layout) this change to the v2 and v1

GaF
  • 101
  • 2
  • 5

1 Answers1

1

Just to get you up and running, you could use the @Published property wrapper and for your example you actually don't need @EnvironmentObject. You can use @ObservedObject instead...

class Pessoa: ObservableObject {
    @Published var data: Array<String>
    
    init() {
        self.data = Array()
        self.data.append("Btn")
    }
}
struct view2: View {
    
    var data: Pessoa
    
    var body: some View {
        
        Button(action: {
            
            self.data.data[0] = "New btn Text"
        }){
            Text("Edit Btn Texr")
            
        }
        
    }
}
struct ContentView: View {
    
    @ObservedObject var data = Pessoa()
    
    var body: some View {
        NavigationView{
            
            VStack{
                
                NavigationLink(destination:view2(data: data)){
                    Text(data.data.first ?? "N/A")
                }
            }
        }
    }
}

But you should check the link of Joakim...

finebel
  • 2,227
  • 1
  • 9
  • 20