UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

ObservableObject in MacOS

Forums > macOS

Hi,

I have written a iOS App and it works very well.

When a user changes a Toggle within a Settings View, the ContentView List also updates no problems.

For example: When showing a list of data, the user has the option to not show a field like say a date, or the name, etc.. by simply opening a Settings view and Toggling a switch to off, so not to display the field.

So when the user toggles it Switch in Settings the List within the ContentView changes and the field that they wish to show or not show updates, straight away !! Wooo hooo.

However... Yep.. However..

Using the same code within my Mac App (Mac only App) it reads the same data from iCloud and updates the Coredata with the list.. just fine there.

I have three View in one Window for the Mac App, the left side is the list, the right side is the entry for new names etc and at the bottom of the screen is a couple of Switches within the Settings View.

I was expecting seeing that I was using the same code as the ios app that all would work.. The Mac App Builds and Runs, but !

When I toggle the Switch to say not display a name field, it does not update the List within the ContentView List.

The only way it works is to shut down the app an reload it, then the name field within the List is not showing which is correct but I expected that to work as soon as I click on the Check box (Toggle)

Some of my code: Sorry about my messy code.. I'm an Old Newby !!

Cheers Craig.

import SwiftUI import Foundation

class SettingsViewModel: ObservableObject {

@Published var showName: Bool = UserDefaults.standard.bool(forKey: "showName") { didSet { UserDefaults.standard.set(self.showName, forKey: "showName") }}

import SwiftUI

struct Settings: View {

@ObservedObject var settingsVM = SettingsViewModel()

var body: some View { VStack { Divider() HStack {

Label("Settings ", systemImage: "gear") .font(.headline) .padding(.leading, 10) Spacer() } HStack{

Toggle(isOn: self.$settingsVM.showName) { Text("Show Name in List:") }.padding(.leading, 10).padding(.trailing, 10)

} .padding(.leading, 10) Spacer() }

} } }

import SwiftUI import CoreData

struct ContentView: View { @Environment(.managedObjectContext) var viewContext

@FetchRequest(entity: Stuff.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Stuff.timeStamp, ascending: false)], animation: .default) private var items: FetchedResults<Stuff>

@ObservedObject var settingsVM = SettingsViewModel()

var body: some View {

VStack{

  HStack(alignment: .center){
    VStack{
      Text("List")
        .font(.title)
        .fontWeight(.semibold)
        .padding(.all)
      Divider()
      //  Spacer()

      List {
        ForEach(items, id: \.self) { item in

          HStack(alignment: .center) {

            Text(item.callsign ?? "Empty")
              .fontWeight(.light)
              .frame(width: 100.0, alignment: .topLeading)

            if settingsVM.showName == true {
            Text(item.name ?? "Empty")
              .fontWeight(.light)
              .frame(width: 70.0, alignment: .topLeading)
            }

            Text(item.location ?? "Empty")
              .fontWeight(.light)
              .frame(width: 120.0, alignment: .topLeading)

            Text(item.band ?? "Empty")
              .fontWeight(.light)
              .frame(width: 70.0, alignment: .topLeading)

            Text("\(item.signalValue)")
              .fontWeight(.light)
              .frame(width: 40.0, alignment: .topLeading)

            Text("\(item.readValue)")
              .fontWeight(.light)
              .frame(width: 40.0, alignment: .topLeading)

            if item.isFavourite == true {
              Text("♥️")
              //frame(width: 40.0, alignment: .topLeading)
            }
            Spacer()

          }
        }
        .onDelete(perform: deleteItems)
      }
      .frame(width: nil, height: 400)
    }
    CreateEntry()
  }

  Settings()

}

}

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.