Hi Dev Forum
I have this piece of SwiftUI code that present an Input field for the user on iOS, Mac to enter an IP address, the entered IP I want to save in a variable so it can be inserted in my further http requests to a server API but it seems like I'm not doing it right.
I then have this code somewhere else in same file, but this is in a new "struct"
How can I make the user entered "CTRLIP" equal to my let statement for servIp?
Any help with this would be great.
Thanks
Me
I have this piece of SwiftUI code that present an Input field for the user on iOS, Mac to enter an IP address, the entered IP I want to save in a variable so it can be inserted in my further http requests to a server API but it seems like I'm not doing it right.
Code Block import SwiftUI // Input of Controller IP struct ServerIPInput: View { @State var CTRLIP: String = "" var body: some View { VStack(alignment: .leading) { Text("Enter Control IP") .font(.callout) .bold() TextField("Enter Control IP...", text: $CTRLIP) .textFieldStyle(RoundedBorderTextFieldStyle()) }.padding() } }
I then have this code somewhere else in same file, but this is in a new "struct"
Code Block let servIp = "$CTRLIP" // here I would need this entered IP
How can I make the user entered "CTRLIP" equal to my let statement for servIp?
Any help with this would be great.
Thanks
Me
@Enevold,
I looked at what you have, and it did confirm what I was thinking that you are trying to use global variables. I would suggest refactoring your code and look into some tutorials on how SwiftUI handles the movement of data. Below I have modified your program some:
I removed your ServerIPInput struct because I didn't see where you called this view ContentView. In the example above, servIp will be whatever the user enters into the textfield; otherwise, it will be keep the initial value of 192.168.0.196. If you want another struct view, you should look into how to bind to a state variable to create a connection for passing data between views. My final suggestion would be looking into making an object that contains all the server logic. For example, you can make an observable object that contains the sendHttpPostDesktops function. This way, it separates your view and data logic.
I looked at what you have, and it did confirm what I was thinking that you are trying to use global variables. I would suggest refactoring your code and look into some tutorials on how SwiftUI handles the movement of data. Below I have modified your program some:
Code Block Swift struct ContentView: View { @State private var servIp: String = "192.168.0.196" let port: Int = 8000 let cmd: String = "xmlcommand" var body: some View { VStack(alignment: .leading) { HStack { Text("Enter Control IP") TextField("Enter Control IP...", text: $servIp) .textFieldStyle(RoundedBorderTextFieldStyle()) } Button(action: { sendHttpPostDesktops() }, label: { Text("Desktops") }).buttonStyle(PlainButtonStyle()) } } func sendHttpPostDesktops() { print("servIP = \(servIp)") } }
I removed your ServerIPInput struct because I didn't see where you called this view ContentView. In the example above, servIp will be whatever the user enters into the textfield; otherwise, it will be keep the initial value of 192.168.0.196. If you want another struct view, you should look into how to bind to a state variable to create a connection for passing data between views. My final suggestion would be looking into making an object that contains all the server logic. For example, you can make an observable object that contains the sendHttpPostDesktops function. This way, it separates your view and data logic.