无法从另一个视图 SwiftUI 访问数组元素

Can not Reach to Array Element from Another View SwiftUI

GitHub Project Link

我有一个 creditCard 模型和 CreditCards class,它具有附加 creditCard 的功能。

class CreditCard: ObservableObject, Identifiable {
@Published var _id: String = UUID().uuidString
@Published var _cardOwnerName : String = ""
@Published var _cardNumber: String = ""
@Published var _cardExpMonth: String = ""
@Published var _cardExpYear: String = ""
@Published var _cardName: String = ""
@Published var _ccv: String = ""

@Published var items = [CreditCard]()

init(cardOwnerName: String, cardNumber: String, cardExpMonth: String,cardExpYear: String, cardName:String, ccv:String)
    {
        self._cardOwnerName = cardOwnerName
        self._cardNumber = cardNumber
        self._cardExpMonth = cardExpMonth
        self._cardExpYear = cardExpYear
        self._cardName = cardName
        self._ccv = ccv
    }
 }

class CreditCards: ObservableObject {
@Published var items = [CreditCard]()


func appendNewCreditCard(creditCard: CreditCard) {
    objectWillChange.send()
    items.append(CreditCard(cardOwnerName: creditCard._cardOwnerName, cardNumber: creditCard._cardNumber, cardExpMonth: creditCard._cardExpMonth, cardExpYear: creditCard._cardExpYear, cardName: creditCard._cardName, ccv: creditCard._ccv))
   }
}

当通过按钮满足所有条件时,我正在附加一张新信用卡。

struct CardInfo : View {
@State var creditCard : CreditCard
@ObservedObject var creditCards = CreditCards()

  CreditCardTextField(data: $cardNumberText, tFtext: "Kredi Kart Numarasi", tFImage: "credit")
                .textContentType(.oneTimeCode)
                .keyboardType(.numberPad)
                .onReceive(Just(cardNumberText)) { cardNumberdata in
                    if cardNumberdata.count == 16 {
                        self.creditCard._cardNumber = self.cardNumberText
                    }
            }

  Button(action: {
                    self.isSaved.toggle()
                    if self.creditCard._cardNumber == "" {
                        self.showingAlert = true
                    } else if self.creditCard._cardNumber.count == 16 {
                        self.successAlert = true
                        self.creditCards.objectWillChange.send()
                        self.creditCards.appendNewCreditCard(creditCard: creditCard)
                        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                           self.successAlert = false    // hide popup
                           self.showAddressView = true    // eg. activate navigation link
                        }
                    }
                }

当我尝试在我的 PaymentMethodView 中显示如下文本时,我在 PaymentMethodView 上看不到任何文本。

struct PaymentMethodView: View {
@ObservedObject var creditCards = CreditCardArray()
var body: some View {
    VStack {
        TopBarPM()
        MiddleView()
            .padding()
        DetailLine()
        VStack {
            HStack {
                Image("credit")
                VStack {
                    ForEach(creditCards.items) { item in
                        Text(item._cardName)
                }
            }
        }
        Spacer()
    }
}

}

现在我需要在 PaymentMethodView 的另一个视图中显示这些信用卡

谢谢分享。 AddCreditCardViewPaymentMethodView(您引用 CreditCardArray 的地方)似乎是两个完全独立的视图,它们不会相互调用。因此我建议使用 CreditCardArray 作为 EnvironmentObject.

目前您正在两个视图中创建新的 ObservedObject CreditCardArray,因此您没有任何数据可显示在 PaymentMethodView.

使用 EnvironmentObject 时,您可以将其注入包含 AddCreditCardViewPaymentMethodView 的视图中。只需为其选择 SceneDelegate 并替换行:

let contentView = ContentView()

let contentView = ContentView().environmentObject(CreditCardArray())

在这里您正在创建一个 CreditCardArray 的对象,您可以在整个应用程序中访问它。

您现在可以通过替换以下行(在 AddCreditCardViewPaymentMethodView 中)来访问此对象:

@ObservedObject var creditCards = CreditCardArray()

@EnvironmentObject var creditCards: CreditCardArray

为了访问之前创建的数组而不是创建一个新数组。