【发布时间】:2020-08-06 12:06:57
【问题描述】:
我的 if else 语句检查某些文本字段是否为空,如果是则弹出警报。然而,xcode 即使经历了一切,也会转移到其他功能。
有一个 if 语句检查分段控件的值并相应地检查一些文本字段。
@IBAction func calc(_ sender: Any) {
// Check if dilution text field is empty
let dilutiontext = self.dilution.text
if (dilutiontext?.isEmpty ?? true) {
Alert.showAlert(on: self, with: "Empty Fields", message: "Dilution field is empty")
}
if choose.selectedSegmentIndex == 0 {
if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true {
Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-4 fields should not be empty")
} else {
performSegue(withIdentifier: "turner", sender: self)
}
} else {
if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true {
Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-8 fields should not be empty")
} else {
performSegue(withIdentifier: "turner", sender: self)
}
}
}
我有另一个控制警报的文件 alert.swift:
import Foundation
import UIKit
struct Alert {
public static func showAlert(on vc: UIViewController, with title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
vc.present(alert, animated: true)
}
}
编辑:
以前的 self.dilution.text?.isEmpty 现在让稀释文本 = self.dilution.text 和稀释文本?isEmpty
我注释掉了准备转场功能,令人惊讶的是,警报开始起作用了。我仍然需要该功能并且警报仍然有效。这是函数:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! SecondViewController
if choose.selectedSegmentIndex == 0 {
vc.n1 = Int(number1.text!)!
vc.n2 = Int(number2.text!)!
vc.n3 = Int(number3.text!)!
vc.n4 = Int(number4.text!)!
vc.dil = Int(dilution.text!)!
vc.cn = Int(choose.selectedSegmentIndex)
} else {
vc.n1 = Int(number1.text!)!
vc.n2 = Int(number2.text!)!
vc.n3 = Int(number3.text!)!
vc.n4 = Int(number4.text!)!
vc.n5 = Int(number5.text!)!
vc.n6 = Int(number6.text!)!
vc.n7 = Int(number7.text!)!
vc.n8 = Int(number8.text!)!
vc.cn = Int(choose.selectedSegmentIndex)
vc.dil = Int(dilution.text!)!
}
}
当我运行它时,它不会显示警报(检查文本字段是否为空),而是继续执行 segue 函数并在展开可选值时显示 Unexpectedly found nil,这是预期的
【问题讨论】:
标签: swift segue uisegmentedcontrol uialertcontroller