Search code examples
swiftuitextfieldviewdidload

Calling viewDidLoad in UITextField


What's the code for calling viewDidLoad in UITextField, I only seem to find mixed answers:

    @IBOutlet weak var providerForm: UITextField!

// What to insert here to call the viewDidLoad function?

    override func viewDidLoad() {
    super.viewDidLoad()

    self.viewDidLoad()
    self.blockchain = Blockchain(genesisBlock: genesisBlock)
    // Do any additional setup after loading the view.
    self.generateDummyTransactions()
    self.providerForm = providerForm
    self.destinatorForm = destinatorForm
    self.amountSum = amountSum

func generateDummyTransactions() {



    let transaction = Transaction(from: "Mary", to: "Steve", amount: 20.0, transactionType: TransactionType.domestic)
    let block1 = Block()
    block1.addTransaction(transaction: transaction)
}

Solution

  • Call generateDummyTransactions() from sendButton's IBAction method and then fetch the textField's text there, i.e.

    class VC: UIViewController {
        @IBOutlet weak var providerForm: UITextField!
    
        @IBAction func onTapSendButton(_ sender: UIButton) {
            self.generateDummyTransactions()
        }
    
        func generateDummyTransactions() {
            if let text = self.providerForm.text {
                //use text here...
            }
        }
    }