0

I am trying to use ObservableObject to transfer data from a content view to a view controller. so I create all needed code. but at the end I got error'Class ViewController has no initializers'

I don't know where I do wrong, actually I had already initialized them

my error

Class 'KeyboardViewController' has no initializers
1. Stored property 'toggle1' without initial value prevents synthesized initializers

my code

class KeyboardViewController: UIInputViewController {

    @Binding var toggle1:Bool
    @Binding var toggle2:Bool
    @Binding var toggle3:Bool
    @Binding var toggle4:Bool

    func HiddeTone1Button(containerview: UIStackView){
            Tone1Button.isHidden = toggle4
    }

and

import SwiftUI

struct ContentView: View {

    fileprivate let application = UIApplication.shared
    @State private var background1 = Color.gray
    @State private var background2 = Color.gray
    @State private var background3 = Color.gray
    @ObservedObject var TransferData = TranferData()

and


import Combine

final class TranferData: ObservableObject {
    @Published var toggle1: Bool = false
    @Published var toggle2: Bool = false
    @Published var toggle3: Bool = false
    @Published var toggle4: Bool = false
}

dennis li
  • 21
  • 4
  • You must provide default values to toggle1...4 or make them `optional`. – Timmy Sep 14 '22 at 13:28
  • 1
    `@Binding` is only for SwiftUI Views not for a UIKit `class`. Try the [Apple SwiftUI Tutorials](https://developer.apple.com/tutorials/swiftui) it is obvious that you are pretty new to SwiftUI, there many little issues with your code that will cause big headaches. – lorem ipsum Sep 14 '22 at 13:28
  • @Timmy, Thanks. can you suggest how could I do that ? – dennis li Sep 14 '22 at 16:29
  • @loremipsum, you are right, I am very new to be a Coder, about half year. thanks for your suggestion, I will study Apple SwiftUI tutorials then. – dennis li Sep 14 '22 at 16:31

0 Answers0