在这个PreferencesVC上,我有两个pickerViews供用户选择两种货币发送到主用户界面。在保存更改和发送数据时,它说意外地发现nil。为什么我的pickerViews总是有来自非空元组的数据?
protocol ChangeCurrencyDelegate {
func userEnteredNewCurrency(top: CurrencyTuple, bottom: CurrencyTuple)
}
class ExchangePreferencesVC: UIViewController {
@IBOutlet weak var topCurrencyLabel: UILabel!
@IBOutlet weak var topCurrencyPicker: UIPickerView!
@IBOutlet weak var bottomCurrencyLabel: UILabel!
@IBOutlet weak var bottomCurrencyPicker: UIPickerView!
var delegate: ChangeCurrencyDelegate?
var topPickerOptions = [(name: String, symbol: String, flag: UIImage)]()
var bottomPickerOptions = [(name: String, symbol: String, flag: UIImage)]()
var userCurrencyChoice: Currencies!
override func viewDidLoad() {
super.viewDidLoad()
topPickerOptions = currencyISOCode
bottomPickerOptions = currencyISOCode
}
// User top and bottom pickerView choices
func createTopAndBottomCurrency() {
let topUserIndex = topCurrencyPicker.selectedRow(inComponent: 0)
let bottomUserIndex = bottomCurrencyPicker.selectedRow(inComponent: 0)
let topUserChoice = currencyISOCode[topUserIndex]
let bottomUserChoice = currencyISOCode[bottomUserIndex]
userCurrencyChoice = Currencies(top: topUserChoice, bottom: bottomUserChoice)
}
// MARK: - Cancel and Save Changes actions
@IBAction func saveChanges(_ sender: Any) {
delegate?.userEnteredNewCurrency(top: userCurrencyChoice.top, bottom: userCurrencyChoice.bottom)
self.dismiss(animated: true, completion: nil)
}
}
字符串
委托是这样分配的->
// MARK: - Delegate of the ChangeCurrency protocol
extension ExchangeRateViewController: ChangeCurrencyDelegate {
func userEnteredNewCurrency(top: CurrencyTuple, bottom: CurrencyTuple) {
self.topFlag.setImage(top.flag, for: .normal)
self.topSymbol.text = top.symbol
self.bottomFlag.setImage(bottom.flag, for: .normal)
self.bottomSymbol.text = bottom.symbol
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToPreferences" {
let destinationVC = segue.destination as! ExchangePreferencesVC
destinationVC.delegate = self
}
}
}
型
1条答案
按热度按时间33qvvth11#
这是你的
prepare
方法。字符串
考虑一下如果segue的标识符不完全匹配
goToPreferences
会发生什么。segue仍然会发生,但委托不会被设置。所以检查你的segue标识符,并添加一些代码来处理你不识别标识符的情况。至少,你应该记录一条消息,这样你就可以更容易地发现问题。