For my message screen, I am wanting the user to pick between either World Wide or the U.S. but it’s not working.Here is the code
struct MessageView: View {
// State variables to store the values of the TextField and DatePicker
@State var name = ""
@State var birthday = Date()
@State var location = "World Wide,U.S."
@State var message = ""
var body: some View {
Form {
// Name
Section(header: Text("Name")) {
TextField("Enter Name", text: $name)
}
// Birthday
Section(header: Text("Birthday")) {
DatePicker("Special Day Date", selection: $birthday, in: ...Date(), displayedComponents: .date)
// in: ...Date() refers to the range the user can select from
// It wouldn't make sense if the user could select a date that hasn't happened yet
// By default a DatePicker will let the user pick a time and date
// displayedComponents: .date, restricts it to date selection only
//Location
Section(header: Text("Location")) {
TextField("World Wide or U.S", text: $location)
// Message
Section(header: Text("Message")) {
TextField("Enter your message", text: $message)
}
}
}
}
The code you have provided inserts a text field for the user to type in their location.
If you want the user to pick from a drop down list of locations instead then you need to use a “Picker” instead of textField.
If you Google “swiftUI picker” then there will be some tutorials to help with pickers.
Once you’ve put the picker in then let us know if it doesn’t work and will try and help
import SwiftUI
struct MessageView: View {
// State variables to store the values of the TextField and DatePicker
@State var name = ""
@State var birthday = Date()
@State var location = "World Wide,United States."
@State var message = ""
var sports = ["World Wide", "United States"]
@State private var selection = 0
var body: some View {
Form {
// Name
Section(header: Text("Name")) {
TextField("Enter Name", text: $name)
}
// Birthday
Section(header: Text("Birthday")) {
DatePicker("Special Day Date", selection: $birthday, in: ...Date(), displayedComponents: .date)
// in: ...Date() refers to the range the user can select from
// It wouldn't make sense if the user could select a date that hasn't happened yet
// By default a DatePicker will let the user pick a time and date
// displayedComponents: .date, restricts it to date selection only
//Location
Picker(selection: $selectedlocation, label: Text("location"))
ForEach(0..< location.count) {
Text(self.location[$0].tag($0)
})
} .pickersyle(SegmentedPickerStyle())
WheelPickerStyle
Text("Current Selection: \(location[selectedlocation]))
}
// Message
Section(header: Text("Message")) {
TextField("Enter your message", text: $message)
}
}
}
Button("Next") {
//go to End view
}
.padding(.all)
.font(.title)
.shadow(radius: 2)
foregroundColor(Color.black)
struct MessageView_Previews: PreviewProvider {
static var previews: some View {
MessageView()
}
}
There’s a few different problems here for us to work through. I suggest we tackle them one at a time. When fixing them, the aim will be to remove the red error message one a time. We’ll need to fix a few things to get rid of all of the red.
So shall we start with line 38 - “cannot find $selectedLocation in scope”. This is quite a common error message and it means that you haven’t declared a variable called selectedLocation. This needs to be a State variable (string) and you can have it start as blank.
Can you fix that and let me know if that error on line 38 goes away?
I’m confused on what you mean by “you haven’t declared a variable called selectedLocation”, I have the" @Statevar location = “World Wide,United States.” for the location
Your code on line 38 tells the picker to use a variable called selectedLocation to store the users selection into. You don’t have a variable called selectedLocation.
If you want to use the location variable for this then you have to change line 38.
Oh I see. So every time the user uses the app, that selection will be the first one to pop
I don’t want that because I want them to be able to pick whichever one you want each time they use it
I’m sorry, i’m confused. Let me re-explain this. With my app, the user will get to decide whether they want to message someone in the U.S. or Worldwide, I just don’t want it to be already selected for them if you come back on the app. I want them to choose between the two options each time
The location variable will be used to store the users selection temporarily. It will be reset each time the user uses the app / send a message. They will be able to select USA or worldwide each time they use the app.
However, You need to store it temporarily in location variable as you’ll presumably need to use this value in next screen / onward processing so the message goes to the right people (either US people or worldwide)
Ok next one - line 39.
This line is to tell the picker what values to show in th e picker (in your case, this will be your two choices of USA or worldwide). The variable on line 39 should be an array of choices.
You have created a variable which is an array of choices which you have called sports (presumably from whatever tutorial you copied). I suggest calling this something more appropriate for your use and then use this on line 39 instead of location.
That should get rid of that error.
Th error on line 41 is because you are missing a ) at the end of line 40
So I changed it at the top, to USA instead of United States and it says “cannot find ‘USA’ in scope” and the error code is now two error codes on line 41 even after I added the ) to line 40
import SwiftUI
struct MessageView: View {
// State variables to store the values of the TextField and DatePicker
@State var name = ""
@State var birthday = Date()
@State var location = "WorldWide, USA"
@State var message = ""
var Location = ["WorldWide", "USA"]
@State private var selection = 0
var body: some View {
Form {
// Name
Section(header: Text("Name")) {
TextField("Enter Name", text: $name)
}
// Birthday
Section(header: Text("Birthday")) {
DatePicker("Special Day Date", selection: $birthday, in: ...Date(), displayedComponents: .date)
// in: ...Date() refers to the range the user can select from
// It wouldn't make sense if the user could select a date that hasn't happened yet
// By default a DatePicker will let the user pick a time and date
// displayedComponents: .date, restricts it to date selection only
//Location
Picker(selection: $location, label: Text("location"))
ForEach(USA,WorldWide) {
Text(self.location[$0].tag($0))
})
} .pickersyle(SegmentedPickerStyle())
WheelPickerStyle
Text("Current Selection: \(location[selectedlocation]))