本文介绍了Swift:NavigationLink 多次调用目的地的 init 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 SubmitPhoneView 和 VerifyPhoneView.

I have two classes SubmitPhoneView and VerifyPhoneView.

出于某种原因,我注意到每当我在 SubmitPhoneView 的文本字段中输入一个数字时,它都会调用 VerifyPhoneView 的 init 方法.我希望它只被调用一次(当我按下 SubmitPhoneView 上的继续按钮时)

For some reason, I noticed that whenever I input a digit into the textfield of SubmitPhoneView, it calls the init method of VerifyPhoneView. I want it to only be called once (when I press the continue button on SubmitPhoneView)

为什么会这样?

类 SubmitPhoneView:

Class SubmitPhoneView:

import SwiftUI
import Firebase

struct SubmitPhoneView: View {

    @State private var phoneNumber: String = ""
    @State private var verificationID : String = ""
    @State private var presentMe = false

    var body: some View {

        ZStack {

            Text("My number is")

            HStack(spacing: 20){
                Text("+1")

                TextField("Enter phone number", text: $phoneNumber)
                    .keyboardType(.numberPad)
            }

            VStack {

                NavigationLink(destination: VerifyPhoneView(phoneNumber: $phoneNumber.wrappedValue, verificationID: $verificationID.wrappedValue), isActive: $presentMe) { EmptyView() }

                Button(action: {
                    self.submitPhoneNumber()
                    self.presentMe = true
                }) {
                    Text("Continue")
            }
        }
    }

    func submitPhoneNumber() {
        PhoneAuthProvider.provider().verifyPhoneNumber("+1" + phoneNumber, uiDelegate: nil) { (verificationID, error) in
            if error != nil {
                print(error.debugDescription)
                return
            }
            else {
                self.verificationID = verificationID!
            }
        }
    }

}

类VerifyPhoneView:

Class VerifyPhoneView:

import SwiftUI
import Firebase

struct VerifyPhoneView: View {

    private var phoneNumber: String
    @State private var verificationID: String
    @State private var verificationCode: String = ""
    @State private var loginSuccesful: Bool = false
    @EnvironmentObject var ls: LoginStatus
    @EnvironmentObject var currentUser: CurrentUser

    init(phoneNumber: String, verificationID: String) {
        print("the init method was called for VerifyPhoneView")
        self.phoneNumber = phoneNumber
       _verificationID = State(initialValue: verificationID)
        print(self.verificationID)
    }

    var body: some View {

        ZStack {

            Text("My code is")

            TextField("Enter code", text: $verificationCode)

            Button(action: {
                self.submitVerificationCode()
            }) {
                Text("Continue")
            }

        }

    }

    func submitPhoneNumber() {
       // doesn't matter
    }

    func submitVerificationCode() {
       // doesn't matter
    }

    }    

}

推荐答案

使用DeferView,如下图

VStack {

    NavigationLink(destination: DeferView {
       VerifyPhoneView(phoneNumber: $phoneNumber.wrappedValue, verificationID: $verificationID.wrappedValue) 
    }, isActive: $presentMe) { EmptyView() }

    Button(action: {
        self.submitPhoneNumber()
        self.presentMe = true
    }) {
        Text("Continue")
}

这篇关于Swift:NavigationLink 多次调用目的地的 init 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 03:23