使用@Binding var初始化SwiftUI类

时间:2020-07-21 02:47:22

标签: class binding swiftui init

我有一个要在另一个View中设置的Binding var初始化的类。

查看->

struct CoverPageView: View {
@State  var numberOfNumbers:Int

   var body: some View {
            NavigationView {
                GeometryReader { geometry in
                    VStack(alignment: .center, spacing: 0){ 
                         TextField("Multiplication Upto:", value: self.$numberOfNumbers, formatter: NumberFormatter())

}
}
}
}

CLASS WHICH NEEDS TO BE INITIALIZED USING THE @Binding var $numberofNumbers - 

import SwiftUI

class MultiplicationPractice:ObservableObject {
    
    @Binding var numberOfNumbers:Int
var classNumofNumbers:Int

init() {
self.classNumofNumbers = self.$numberOfNumbers
}
}

init语句显然会给出以下错误:self未初始化,实例var用于初始化,这是不允许的。

我该如何规避?该类需要使用用户在第一个视图上输入的数字进行初始化。我写了大约。代码,因此请忽略任何错字。

2 个答案:

答案 0 :(得分:0)

好的,我不太了解您的问题,因此我只列出一些示例,希望其中一个是您要查找的示例。


struct SuperView: some View {
    @State var value: Int = 0

    var body: some View {
        SubView(value: self.$value)
    }
}
struct SubView: View {
    @Binding var value: Int

    // This is the same as the compiler-generated memberwise initializer
    init(value: Binding<Int>) {
        self._value = value
    }

    var body: some View {
        Text("\(value)")
    }
}

如果我误解了,而您只是想获取当前值,请执行此操作

struct SuperView: some View {
    @State var value: Int = 0

    var body: some View {
        SubView(value: self.value)
    }
}
struct SubView: View {
    let value: Int

    // This is the same as the compiler-generated memberwise initializer
    init(value: Int) {
        self.value = value
    }

    var body: some View {
        Text("\(value)")
    }
}

答案 1 :(得分:0)

通常,您会在CoverPageView中使用起始值初始化MultiplicationPractice:

@ObservedObject var someVar = MultiplicationPractice(NoN:123)

当然,还要添加一个支持的init语句:

class MultiplicationPractice:ObservableObject {
    init(NoN: Int) {
        self.numberOfNumbers = val
    }

,并且您不想使用@Binding包裹var,而是使用@Published包裹:

class MultiplicationPractice:ObservableObject {

@Published var numberOfNumbers:Int
...

在您的特定情况下,我什至将numberOfNumbers变量放在您的CoverPageView中,而是使用上述someVar的直接变量:

struct CoverPageView: View {
    //removed @State  var numberOfNumbers:Int
    @ObservedObject var someVar = MultiplicationPractice(123)
    
    ...
    
    TextField("Multiplication Upto:", value: self.$someVar.numberOfNumbers, formatter: NumberFormatter())

您会注意到,我传入了@ObservedObject的子变量作为绑定。我们可以使用ObservableObjects做到这一点。


编辑

我现在看到了您要执行的操作,您想在ViewModel中传递绑定,并在视图和模型之间建立间接连接。尽管这可能不是我个人的方式,但我仍然可以提供一个有效的示例。

以下是使用您的结构名称的简单示例:

struct MultiplicationGame {
    @Binding var maxNumber:String
    init(maxNumber: Binding<String>) {
        self._maxNumber = maxNumber
        print(self.maxNumber)
    }
}

class MultiplicationPractice:ObservableObject {
    var numberOfNumbers: Binding<String>
    @Published var MulGame:MultiplicationGame
    
    init(numberOfNumbers: Binding<String> ) {
        self.numberOfNumbers = numberOfNumbers
        self.MulGame = MultiplicationGame(maxNumber: numberOfNumbers)
    }
    
}
struct ContentView: View {
    @State var someText: String
    @ObservedObject var mulPractice: MultiplicationPractice
    init() {
        let state = State(initialValue: "")
        self._someText = state
        self.mulPractice = MultiplicationPractice(numberOfNumbers: state.projectedValue)
    }
    var body: some View {
        TextField("put your text here", text: $someText)
    }
}