Here is the full error:
`Fatal error: No ObservableObject of type LocationsViewModel found. A View.environmentObject(_:) for LocationsViewModel may be missing as an ancestor of this view.`
I am essentially creating an app. The user can login/register. Once the user logs in they see a map view (MapKit). Here is just the main code (excluding the login/register parts). Any idea what i am doing wrong?
// MARK: MAP SCREEN
struct UserScreen: View {
@Binding var sucessLogin : Bool
@StateObject private var vm = LocationsViewModel()
var body: some View{
ZStack{
Map(coordinateRegion: $vm.mapRegion)
.ignoresSafeArea()
VStack(spacing: 0){
VStack{
Button(action: vm.toggleLocationsList){
Text(vm.mapLocation.name + ", " + vm.mapLocation.cityName)
.font(.title2)
.fontWeight(.black)
.foregroundColor(.white)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Color("Theme"))
.overlay(alignment: .leading){
Image(systemName: "arrow.down")
.font(.headline)
.foregroundColor(.white)
.padding()
}
}
if vm.showLocationsList{
LocationsListView()
}
}
.background(.thickMaterial)
.cornerRadius(10)
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 15)
.padding()
Spacer()
}
}
.navigationTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
I then have a LocationsViewModel which is in a different SwiftUI View file because I have a custom dropdown menu, here is the code:
// MARK: MAP Screen ViewModel
class LocationsViewModel: ObservableObject{
@Published var locations: [Location]
@Published var mapLocation: Location{
didSet{
updateMapRegion(location: mapLocation)
}
}
//Show current region on map
@Published var mapRegion: MKCoordinateRegion = MKCoordinateRegion()
let mapSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
// Show list of locations
@Published var showLocationsList: Bool = false
init(){
let locations = LocationsDataService.locations
self.locations = locations
self.mapLocation = locations.first!
self.updateMapRegion(location: locations.first!)
}
private func updateMapRegion(location: Location){
withAnimation(.easeInOut){
mapRegion = MKCoordinateRegion(
center: location.coordinates,
span: mapSpan)
}
}
func toggleLocationsList(){
withAnimation(.easeInOut){
showLocationsList.toggle()
}
}
}
Not sure what the error means and how I can fix it, because in my ViewModel i have it extended to Observable Object.
If you would like I can also provide a GitHub link to my project.
Post
Replies
Boosts
Views
Activity
I want to blur the button if the condition inside .disable(condition) is true. How can i do this in swiftui?
For example.
Button(action: {
// What to perform
}) {
// How the button looks like
}
.disable(tnum < 100)
at the same time if tnum count goes under 100, i want the button to be blured as well.
how can i achieve this?
Lets say I have an array of numbers like this:
// Create Array Of Numbers
let numbers = ["1","2","3","4","5"]
If I want to print a random number from the array, I can do something like:
pickedNumber = Int.random(in: 0...numbers.count - 1)
The above line will return a random value from my array.
What I would like to do is, set a probability for each value in the array. For example:
- Chance of 1 being picked at 10%
- Chance of 2 being picked at 20%
- Chance of 3 being picked at 30%
- Chance of 4 being picked at 35%
- Chance of 5 being picked at 5%
What's the best approach for this? Any guidance or advice would be appreciated. This problem I am facing is in swiftUI.
I am creating a to-do list app. I need help or guidance in creating the following UITextInput and UITextView put it into some sort of container to get the rounded corners.
I have tried to create it by implementing a UITableView, creating 2 cells. One with TextInput and one with TextView but it doesn't work, I have a feeling it's a wrong approach or there might be a better way to achieve this.
Any guidance or help would be appreciated.