Search code examples
swiftuidispatch-async

Pass to Another View after Success View Appears


After seeing the popup view that says your credit card saved successfully. I want to see this popup for 2-3 seconds then to pass another view called AddressView. Maybe it is irrelevant but I also added that popup view and the name is SuccessCardView.

@State private(set) var successAlert = false
ZStack {
 HStack {
                Button(action: {
                    self.isListTapped.toggle()
                }, label: { CustomButton(title: "Listeden Sec", icon: .none, status: .enable, width: 150)})
                Button(action: {
                    self.isSaved.toggle()
                    self.creditCards.append(self.creditCard)
                    print(self.creditCards[0].cardNumber)
                    if self.creditCards[0].cardNumber == "" {
                        self.showingAlert = true
                    } else if self.creditCards[0].cardNumber.count == 16 {
                        self.successAlert = true
                    }
                }, label: { CustomButton(title: "Kaydet", icon: .none, status: .enable, width: 150)})
                    .alert(isPresented: $showingAlert) {
                        Alert(title: Text("Kart Bilgileri Hatali"), message: Text("Tekrar Kontrol Edin"), dismissButton: .default(Text("Got it!")))
                }
            }
            SuccessCardView(isShown: $successAlert) // I want to show that view than jump to another view

}

struct SuccessCardView: View {
@Binding var isShown: Bool
@State var viewState = CGSize.zero
var body: some View {
    VStack {
        ZStack {
            Rectangle()
                .foregroundColor(Color(#colorLiteral(red: 0, green: 0.6588235294, blue: 0.5254901961, alpha: 1)))
                .cornerRadius(10)
                .frame(width: 355, height: 76)
            HStack {
                VStack(alignment: .leading) {
                    Text("Tebrikler!")
                        .font(Font.custom("SFCompactDisplay-Bold", size: 16))
                        .foregroundColor(.white)
                    Text("Kart Basariyla Eklendi")
                        .font(Font.custom("SFCompactDisplay", size: 14))
                        .foregroundColor(.white)
                }
                .offset(x: -70)
            }
        }
        Spacer()
    }
    .offset(y: isShown ? .zero : -UIScreen.main.bounds.size.height)
    .offset(y: viewState.height )
    .animation(.spring(response: 0.5, dampingFraction: 0.6, blendDuration: 0))
    .offset(y: -100)
}

}


Solution

  • It is not clear where/how is AddressView configured, but you can do the following

        } else if self.creditCards[0].cardNumber.count == 16 {
            self.successAlert = true
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
               self.successAlert = false    // hide popup
               self.showAddressView = true    // eg. activate navigation link
            }
        }