2

I have a date selector in a view and, once the user enters a date and saves I display a new view with a toggle. Ideally, once the users flips the toggle I'd like to be able to set a reminder using the date field already entered. I have created an ObservableObject

import SwiftUI
import Combine

class UpdateVM: ObservableObject{

    @Published var reminderDate = Date() {didSet {
                 print("set")
    }

which I declare in the View as:

@ObservedObject var updateVM  = UpdateVM()


    if(self.isToggle){
       updateVM.reminderDate = flossTheCat.reminderDate!
    }

I get an error "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols"

This works fine in the action area of a button press but I don't see if it's possible to react to a toggle flip - is the toggle just designed to reflect UI changes and should I implement a button instead ? Definitely having a hard time adjusting to the SwiftUI paradigm even if it makes sense overall

Thanks !

per the request - here is where it does work as I hoped it would (via a button)

trailing: Button(action: {
    do {
        let flossingReminders = FlossingPets.init(context: self.context)
        self.flossingVM.reminderDate = self.flossingDate

        if self.context.hasChanges {
            try self.context.save()
        }
    }catch {
        print(error)
    }
BobdeBloke
  • 149
  • 1
  • 6
  • Can you please show more code? specifically the condition where you set the `updateVM.reminderDate` where is this happening? – Muhand Jumah Jun 10 '20 at 17:46
  • You need proxy binding in Toggle. See [example here](https://stackoverflow.com/a/60507197/12299030) – Asperi Jun 10 '20 at 17:56
  • Asperi - thank you - would this just pass back a bool value and a specific string/flag ? I'm looking to access a date value which could be any date in the future – BobdeBloke Jun 10 '20 at 20:20

0 Answers0