为什么我的 if 函数不能阻止转至下一个视图控制器?
Why are my if functions not preventing the segue to the next view controller?
我有一系列 if 函数来检查两个文本字段是否为空(或其中一个),如果是,则在标签中显示错误消息。如果两个文本字段都不为空,那么它应该执行一个计算,然后转到下面的视图控制器,并在标签中显示等式的值。然而,即使使用 if 函数来检查文本字段是否为 nil,segue 仍然会执行,即使它们为 nil,错误标签也是如此。为什么 if 函数不能阻止这种情况?
@IBAction func CalcFlowRateEnd(_ sender: UIButton) {
let volume = Double(VolTextField.text!)
let time = Double(SecTextField.text!)
let units = volumeUnitsPicker4.selectedRow(inComponent: 0)
var selectedUnit = data[units]
//milliliters error labels
if selectedUnit == "milliliters" && volume == nil && time == nil {
volErrorLabel.text = "Enter Volume"
secErrorLabel.text = "Enter Seconds"}
if selectedUnit == "milliliters" && volume != nil && time == nil {
secErrorLabel.text = "Enter Seconds"}
if selectedUnit == "milliliters" && volume == nil && time != nil {
volErrorLabel.text = "Enter Volume"}
//milliliters calc
if selectedUnit == "milliliters" && volume != nil && time != nil{
let volumeConvert = Double(volume!/1000)
let timeConvert = Double(time! / 60)
let flowRateMetric = Double(volumeConvert / timeConvert)
let volumeImperial = Double(volume!/4546)
let flowRateImperial = Double(volumeImperial / timeConvert)
let roundedMetric = round(flowRateMetric * 100) / 100.0
let roundedImperial = round(flowRateImperial * 100) / 100.0
calcFlowRate.text = "\(roundedMetric) liters per min or \(roundedImperial) gal per min"
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let flowRate = calcFlowRate.text ?? ""
let destinationVC = segue.destination as! FlowRateCalcEndViewController
destinationVC.flowRate = flowRate
}
prepare(for segue)
函数不应嵌套在 @IBAction
函数中。
func prepare(for segue)
应该被覆盖。它在执行 segue 之前调用,因此您有机会“做某事”
var isFlowRateCalculated = false
@IBAction func CalcFlowRateEnd(_ sender: UIButton) {
if selectedUnit == "milliliters" && volume != nil && time != nil {
// logic
isFlowRateCalculated = true
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return isFlowRateCalculated
}
override func prepare(for segue) {
// hand off data for view being presented
}
这并不能解决您的全部问题,但它是朝着正确方向迈出的一步。很高兴继续和你谈论这个:)
我还建议您看看 Medium
上的这篇文章
我有一系列 if 函数来检查两个文本字段是否为空(或其中一个),如果是,则在标签中显示错误消息。如果两个文本字段都不为空,那么它应该执行一个计算,然后转到下面的视图控制器,并在标签中显示等式的值。然而,即使使用 if 函数来检查文本字段是否为 nil,segue 仍然会执行,即使它们为 nil,错误标签也是如此。为什么 if 函数不能阻止这种情况?
@IBAction func CalcFlowRateEnd(_ sender: UIButton) {
let volume = Double(VolTextField.text!)
let time = Double(SecTextField.text!)
let units = volumeUnitsPicker4.selectedRow(inComponent: 0)
var selectedUnit = data[units]
//milliliters error labels
if selectedUnit == "milliliters" && volume == nil && time == nil {
volErrorLabel.text = "Enter Volume"
secErrorLabel.text = "Enter Seconds"}
if selectedUnit == "milliliters" && volume != nil && time == nil {
secErrorLabel.text = "Enter Seconds"}
if selectedUnit == "milliliters" && volume == nil && time != nil {
volErrorLabel.text = "Enter Volume"}
//milliliters calc
if selectedUnit == "milliliters" && volume != nil && time != nil{
let volumeConvert = Double(volume!/1000)
let timeConvert = Double(time! / 60)
let flowRateMetric = Double(volumeConvert / timeConvert)
let volumeImperial = Double(volume!/4546)
let flowRateImperial = Double(volumeImperial / timeConvert)
let roundedMetric = round(flowRateMetric * 100) / 100.0
let roundedImperial = round(flowRateImperial * 100) / 100.0
calcFlowRate.text = "\(roundedMetric) liters per min or \(roundedImperial) gal per min"
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let flowRate = calcFlowRate.text ?? ""
let destinationVC = segue.destination as! FlowRateCalcEndViewController
destinationVC.flowRate = flowRate
}
prepare(for segue)
函数不应嵌套在 @IBAction
函数中。
func prepare(for segue)
应该被覆盖。它在执行 segue 之前调用,因此您有机会“做某事”
var isFlowRateCalculated = false
@IBAction func CalcFlowRateEnd(_ sender: UIButton) {
if selectedUnit == "milliliters" && volume != nil && time != nil {
// logic
isFlowRateCalculated = true
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return isFlowRateCalculated
}
override func prepare(for segue) {
// hand off data for view being presented
}
这并不能解决您的全部问题,但它是朝着正确方向迈出的一步。很高兴继续和你谈论这个:)
我还建议您看看 Medium
上的这篇文章