1

So I have a ViewA which returns a value called idd after defining a function addNote() and I wish to use that idd value in ViewB. I'm able to define idd in ViewB as viewA.addNote() however I'm not able to get the value of idd passed to viewB. And even Xcode says that my result from addNote() us unused. I'm new to swiftUI and learning everyday so I'd appreciate if you could let me know how to approach this problem. Thanks!

View A:

class viewA: ObservableObject{
@Published var idd: String = ""

func addNote()-> String{
  
   // some Code

         return idd

}

}

View B:

   struct ViewB: View {

    @StateObject var viewTwo = viewTwo()

    @State var idd = String()

     var body: some View {

        NavigationView {

            ZStack{
   .navigationTitle("Notes")
                
            .navigationBarItems(
                trailing: Button (action: {},
                                  
 label: { NavigationLink(destination: NoteView(newNote: "", idd: viewA.addNote() ) )
                   
                       
          {Image(systemName: "plus")} }
                     ))
                    .listStyle(.plain)
                    .buttonStyle(PlainButtonStyle())
          
                
                
                }
}
        
alex
  • 118
  • 1
  • 6
  • 2
    Take a couple of SwiftUI tutorials for beginning. – Asperi Jun 29 '22 at 04:53
  • Hi @Asperi , thanks for the input. I've been learning a lot from Hacking with Swift on YouTube and a random course on edX. I feel like I'm doing everything right because my addNote() clearly returns a value called idd and even console is able to log that. But I'm not sure what I do wrong when I call it in another View. Would appreciate any input! – alex Jun 29 '22 at 04:55
  • look-up how to use `@StateObject` and `ObservableObject`. Hint, `@Published` should be in a `ObservableObject` class, such as: `class SomeClass: ObservableObject { @Published var idd: String = "" ....}` – workingdog support Ukraine Jun 29 '22 at 04:56
  • @workingdogsupportUkraine thanks for the input. I just updated my code because I was just trying to use what you just suggested, Observable Object. Turns out it's still not working. – alex Jun 29 '22 at 05:00
  • First of all Please fix the indentation of your code. Also provide code that works. Minimum reproducible code. You just have to set idd from the method of that class (in this case at the add note method). Idd will be auto updated in the views. – Tanvirgeek Jun 29 '22 at 05:24
  • I'm sorry about the indentation. I'll fix that in this edit. I think I see what you're saying but how exalt would you 'set the idd from the method of the addNote() method'? Are there any special methods for that? Pardon my ignorance I'm still learning! – alex Jun 29 '22 at 05:32

1 Answers1

0
  1. viewA is your viewModel for viewB. So the name of viewA should be ViewBViewModel.

  2. class/struct names should start with Capital letters.

  3. In your viewModel (in this case in viewA) just set a value to your Published property , it will be auto updated throughout all the Views where it is being used. In this case:

    class viewA: ObservableObject{
        @Published var idd: String = ""
    
        func addNote()-> String {
    
       // some Code
    
            self.idd = idd // setting idd here, which will be auto updated (since its a published variable) in the 
    //views where @Stateobject property wrapper is being used to instantiate viewA
    // (which is a viewModel actually).
    
        }
    }
    
Tanvirgeek
  • 540
  • 1
  • 9
  • 17
  • Also instantiate viewA in viewB with @StateObject property wrapper like you did. Then access that idd as viewA.idd, which will always get updated values. – Tanvirgeek Jun 29 '22 at 05:42