“视图”初始化程序的结果未使用

时间:2020-09-15 08:48:33

标签: swiftui init

“我的自定义”按钮没有轻击,而是转到下一个称为AddCreditCardView的视图。

我已经用print语句测试了按钮动作,但是它也行不通。

我在下面分别复制了我的代码。

这是我的ContentView

import SwiftUI
struct ContentView: View {
    let membershipRows = MembershipData.listData()
    let corporateRows = CorporateData.listData()
    let otherOperationRows = OtherOperationsData.listData()
    @State var selectedCard = CreditCard(id: "", cardOwnerName: "", cardNumber: "", cardExpMonth: "", cardExpYear: "", ccv: "")
    @State var shown: Bool = false
    var body: some View {
        NavigationView {
            VStack {
                List {
                    Section(header: Text("Bireysel")) {
                        ForEach(membershipRows) { row in
                            NavigationLink(destination: CreditCardView()) {
                                RowElementView(row: row)
                            }
                        }
                    }
                    if self.corporateRows.count == 0
                    {
                        Rectangle()
                            .background(Color(.white))
                            .edgesIgnoringSafeArea(.all)
                            .foregroundColor(.white)
                            .padding(.vertical,32)
                    }
                    else {
                        Section(header: Text("Kurumsal")) {
                            ForEach(corporateRows) { row in
                                RowElementView(row: row)
                            }
                        }
                    }
                    Section(header: Text("Diger Islemler")) {
                        ForEach(otherOperationRows) { row in
                            RowElementView(row: row)
                        }
                    }
                    Rectangle()
                        .foregroundColor(.clear)
                        .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height )
                }
                .navigationBarTitle("Odeme Yontemleri", displayMode: .inline)
                .font(Font.custom("SFCompactDisplay", size: 16))
                Button(action: {
                    AddCreditCardView(item: self.selectedCard)
                }, label: { CustomButton(title: "Odeme Yontemi Ekle", icon: .none, status: .enable)
                })
            }
        }
    }

这是我的AddCreditCardView

import SwiftUI

struct AddCreditCardView: View {
    var item: CreditCard
    
    var body: some View {
        NavigationView {
            VStack {
                TopBar()
                Spacer()
                CardInfo()
                Spacer()
            }
            .navigationBarTitle("Odeme Yontemi", displayMode: .inline)
        }
    }
}

struct TopBar : View {
    var body: some View {
        VStack {
            HStack() {
                Image("addcreditcard")
                Image("line")
                Image("locationBar")
                Image("line")
                Image("check-circle")
            }
            .padding(.horizontal,62)
            VStack {
                Text("Kredi Karti Ekle")
                    .font(Font.custom("SFCompactDisplay-Bold", size: 14))
                Text("1. Adim")
                    .font(Font.custom("SFCompactDisplay", size: 14))
                    .fontWeight(.regular)
                    .foregroundColor(.gray)
            }
        }
        .padding()
    }
}

struct CardInfo : View {
    var body: some View {
        VStack {
            CustomTextField(tFtext: "Kartin Uzerindeki Isim", tFImage: "user")
                .textContentType(.givenName)
            CustomTextField(tFtext: "Kredi Kart Numarasi", tFImage: "credit")
                .textContentType(.oneTimeCode)
                .keyboardType(.numberPad)
            HStack {
                CreditCardDateTextField(tFtext: "", tFImage: "date")
                    .textContentType(.creditCardNumber)
                Spacer()
                    Text("|")
                    .foregroundColor(.black)
                    .overlay(
                        Rectangle()
                            .frame(width: 60, height: 53))
                        
                CustomTextField(tFtext: "CCV", tFImage: "")
                    .textContentType(.creditCardNumber)
            }
            .foregroundColor(Color(#colorLiteral(red: 0.9647058824, green: 0.9725490196, blue: 0.9882352941, alpha: 1)))
            CustomTextField(tFtext: "Kart Ismi", tFImage: "cardEdit")
            Spacer()
        }
        
    }
}

最后,这是我的信用卡模型

import SwiftUI

struct CreditCard: Identifiable {
    var id: String = UUID().uuidString
    var cardOwnerName : String
    var cardNumber: String
    var cardExpMonth: String
    var cardExpYear: String
    var ccv: String

My error screenshot

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试按按钮导航至AddCreditCardView。动作关闭不能像那样自动显示视图!您应该将该代码更改为类似 的内容:

    @State var navigated = false

    ,,,

        NavigationLink("AddCreditCardView", destination: AddCreditCardView(), isActive: $navigated)

        Button(action: { self.navigated.toggle() },
               label: { CustomButton(title: "Odeme Yontemi Ekle", icon: .none, status: .enable) })

更改navigated状态将显示您希望显示的下一页。