GitHub项目链接
我有一个信用卡模型和信用卡类,它有一个附加信用卡的函数。
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 中的另一个中显示这些信用卡
感谢您的分享。似乎AddCreditCardView
和PaymentMethodView
(您引用的是CreditCardArray
(是两个完全分离的视图,它们互不调用。因此,我建议使用CreditCardArray
作为EnvironmentObject
。
目前,您正在两个视图中创建新的ObservedObject
CreditCardArray
,因此您没有任何数据可在PaymentMethodView
中显示。
使用EnvironmentObject
时,您可以将其注入包含AddCreditCardView
和PaymentMethodView
的视图中。只需为其选择SceneDelegate
并替换行:
let contentView = ContentView()
带有
let contentView = ContentView().environmentObject(CreditCardArray())
在这里,您正在创建一个CreditCardArray
对象,您可以在整个应用程序中访问该对象。
您现在可以通过替换以下行(在AddCreditCardView
和PaymentMethodView
中(来访问此对象:
@ObservedObject var creditCards = CreditCardArray()
带有
@EnvironmentObject var creditCards: CreditCardArray
以便访问先前创建的阵列,而不是创建新的阵列。