gpt4 book ai didi

ios - 如何通过 View 设置默认 UITextField 值已加载

转载 作者:行者123 更新时间:2023-11-28 13:48:25 24 4
gpt4 key购买 nike

我正在尝试将默认值从保存到结构的 JSON 数据加载到文本字段。获取JSON数据的函数运行在viewdidload中。我在 viewdidlod 下有代码,它返回 nil 值。测试我在 IBAction 下运行的代码并且运行良好。如何让代码在 viewdidload 中正确运行?

override func viewDidLoad() {
super.viewDidLoad()

setUpPicker(textField: lenderName, pickerView: lenderNamePicker, PickerViewDelegate: self, PickerViewDataSource: self)
setUpPicker(textField: mortgageTerm, pickerView: mortgageTermPicker, PickerViewDelegate: self, PickerViewDataSource: self)

SelectionFunctions.MortgageTermSelection()

getRates (completion: { [weak self] in
self?.lenderName.reloadInputViews()
})

DispatchQueue.main.async {
let setupLender = Data.currentRateData.first(where: {$0.financialInstitution == "CIBC Mortgages"})
let setupRate = setupLender?.fiveYear
self.mortgageRate.text = setupRate?.percentage
print(setupRate)
print(setupLender)
print(Data.currentRateData.first?.financialInstitution)
}

lenderName.text = "CIBC Mortgages"
mortgageAmount.text = "$300,000.00"
orginalStartDate.text = ""
mortgageTerm.text = "5 Year"
//mortgageRate.text = "3.89%"
OrginalDiscount.text = "1.25%"
cashback.text = "$2,000"

lenderName.addDoneButton()
mortgageAmount.addDoneButton()

orginalStartDate.addDoneButton()
mortgageTerm.addDoneButton()
mortgageRate.addDoneButton()
orginalStartDate.addDoneButton()
cashback.addDoneButton()
OrginalDiscount.addDoneButton()

func setupDelegate(textField: UITextField){
textField.delegate = self
}

setupDelegate(textField: mortgageAmount)
setupDelegate(textField: mortgageRate)
setupDelegate(textField: OrginalDiscount)
setupDelegate(textField: cashback)


DatePicker()
}

最佳答案

只需将 DispatchQueue.main.async 的使用移动到 getRates 调用的完成 block 中。

getRates (completion: { [weak self] in
self?.lenderName.reloadInputViews()

DispatchQueue.main.async {
let setupLender = Data.currentRateData.first(where: {$0.financialInstitution == "CIBC Mortgages"})
let setupRate = setupLender?.fiveYear
self.mortgageRate.text = setupRate?.percentage
print(setupRate)
print(setupLender)
print(Data.currentRateData.first?.financialInstitution)
}
})

我假设 getRates 调用正在加载 DispatchQueue.main.async block 中使用的数据。

关于ios - 如何通过 View 设置默认 UITextField 值已加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55080107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com