Accessing variables in one class from other class

I have one child class which contains the username variable. I wanna access it from the parent class. But the variable does not appear when I type. And if I forcefully type Add_new_presets.username, then it throws the following error.

Error: Instance member 'username' cannot be used on type 'Add_new_presets'; did you mean to use a value of this type instead?

Child class

import SwiftUI


struct Add_new_presets: View {

    @State  var username: String = ""
    @FocusState var emailFieldIsFocused :Bool = false


    var body: some View {

            TextField(
                "User name (email address)",
                text: $username
            )
            .focused($emailFieldIsFocused)
            .onSubmit {
                //validate(name: username)
            }
            .textInputAutocapitalization(.never)
            .disableAutocorrection(true)
            .border(.secondary)



        }
    }

Parent class

import SwiftUI
struct ContentView: View {
  var body: some View {
        HStack {

            Add_new_presets()

            Text( Add_new_presets.username)
                .foregroundColor(Add_new_presets.emailFieldIsFocused ? .red : .blue)

        }

    }
}


What you are calling "classes" are actually "structs" (so you may need to read up on the basics of Swift and SwiftUI).

You have two Views, but no Model or ViewModel.

There does not seem to be any need to have Text( Add_new_presets.username) in your ContentView, so the simplest quick fix would be to move that into the Add_new_presets View.

..

The best would effectively be to move this part of code.

But, if you want, you should try this

struct ContentView: View {
    
    var body: some View {
        let myView = Add_new_presets()
        
        VStack {
            myView
            HStack {
                Text("Hello")
                Text(myView.username)
                    .foregroundColor(myView.emailFieldIsFocused ? .red : .blue)
            }
        }
    }
}

Note : I get a compilation error (maybe a SwiftUI bug) with

    @FocusState var emailFieldIsFocused :Bool = false

Needed to remove = false

read this:

https://stackoverflow.com/questions/68073919/swiftui-focusstate-how-to-give-it-initial-value

If you want to access some variables common from within the parent view and from within the child view, you may need to hold the variables in the parent view.

As Swift View is a struct and modifying the variable inside its instance may cause many behaviors you do not expect.

Child View

import SwiftUI

struct AddNewPresets: View {
    
    @Binding  var username: String
    var emailFieldIsFocused: FocusState<Bool>.Binding
    
    var body: some View {
        
        TextField(
            "User name (email address)",
            text: $username
        )
            .focused(emailFieldIsFocused)
            .onSubmit {
                //validate(name: username)
            }
            .textInputAutocapitalization(.never)
            .disableAutocorrection(true)
            .border(.secondary)
    }
}

Parent View

import SwiftUI

struct ContentView: View {
    
    @State  var username: String = ""
    @FocusState var emailFieldIsFocused :Bool
    
  var body: some View {
        HStack {

            AddNewPresets(username: $username, emailFieldIsFocused: $emailFieldIsFocused)

            Text(username)
                .foregroundColor(emailFieldIsFocused ? .red : .blue)

        }

    }
}
Accessing variables in one class from other class
 
 
Q