我有2个UITextFields
:一个是VehiclePlateNumber,另一个是TrailerPlateNumber。它们都是可选的。
但是,如果两者都不见,那么当我单击“下一步”按钮时,我会显示一条消息。
问题是我只能填满1个UITextField
,无法进入下一个屏幕。目前,我需要填写它们两者才能进入下一个屏幕,我不希望这样。
这是我的代码:
class RegistrationNumber {
var vehicleRegistrationNumber: String?
var trailerRegistrationNumber: String?
init(vehicleRegistrationNumber: String?, trailerRegistrationNumber: String?) {
self.vehicleRegistrationNumber = vehicleRegistrationNumber
self.trailerRegistrationNumber = trailerRegistrationNumber
}
}
// I was thinking to use this enum somehow. Or maybe is possible with guardlets ?
enum isValid {
case vehicle
case trailer
case vehicleAndTrailer
}
// When user press Next button
@IBAction func nextBtnTapped(_ sender: UIBarButtonItem) {
let selectInspection = storyboard?.instantiateViewController(withIdentifier: "SelectInspectionVC") as! SelectInspectionVC
selectInspection.receivedRegistrationNumber = RegistrationNumber(vehicleRegistrationNumber: vehicleRegTextField.text, trailerRegistrationNumber: trailerRegTextField.text)
let spinner = UIViewController.displaySpinner(onView: self.view)
if notContainsVehicleReg(){
showAlertWithTitle(message: "Wrong vehicle registration.")
UIViewController.removeSpinner(spinner: spinner)
print("Vehicle don't exist in list !!!")
}
else if notContainsTrailerReg(){
showAlertWithTitle(message: "Wrong trailer registration.")
UIViewController.removeSpinner(spinner: spinner)
print("Trailer don't exist in list !!!")
}
else if vehicleRegTextField.text.isBlank && trailerRegTextField.text.isBlank{
showAlertWithTitle(message: "Vehicle registration or/and Trailer Code required.")
UIViewController.removeSpinner(spinner: spinner)
}
else{
UIViewController.removeSpinner(spinner: spinner)
navigationController?.pushViewController(selectInspection, animated: true)
}
}
fileprivate func notContainsVehicleReg() -> Bool {
let filteredVehicles: [String] = vehiclesArray.map { $0.code.uppercased() }
return !filteredVehicles.contains(vehicleRegTextField.text ?? String())
}
fileprivate func notContainsTrailerReg() -> Bool {
let filteredTrailers: [String] = trailersArray.map { $0.code.uppercased() }
return !filteredTrailers.contains(trailerRegTextField.text ?? String())
}
// Extension to check if an optional String is empty or contain blank spaces
extension Optional where Wrapped == String {
var isBlank: Bool {
if let unwrapped = self {
return unwrapped.isBlank
} else {
return true
}
}
}
// Check if a textfield don't contain white spaces or is empty
extension String {
var isBlank: Bool {
return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
谢谢您的帮助。
答案 0 :(得分:1)
只需将您的nextBtnTapped(:UIBarButton)函数更改为此。 这将检查两个textField是否为空,并且如果其中一个或两个都正确填充,它将转到下一个ViewController。
@IBAction func nextBtnTapped(_ sender: UIBarButtonItem) {
let spinner = UIViewController.displaySpinner(onView: self.view)
if vehicleRegTextField.text.isBlank && trailerRegTextField.text.isBlank{
showAlertWithTitle(message: "Vehicle registration or/and Trailer Code required.")
UIViewController.removeSpinner(spinner: spinner)
} else {
let selectInspection = storyboard?.instantiateViewController(withIdentifier: "SelectInspectionVC") as! SelectInspectionVC
selectInspection.receivedRegistrationNumber = RegistrationNumber(vehicleRegistrationNumber: vehicleRegTextField.text, trailerRegistrationNumber: trailerRegTextField.text)
UIViewController.removeSpinner(spinner: spinner)
navigationController?.pushViewController(selectInspection, animated: true)
}
}
答案 1 :(得分:0)
我像这样固定了这2 UITextFields
的验证:
// Check if VehicleTextField contains any plate number from array of vehicles
fileprivate func notContainsVehicleReg() -> Bool {
let filteredVehicles: [String] = vehiclesArray.map { $0.code.uppercased() }
return !filteredVehicles.contains(vehicleTextField.text ?? String())
}
// Check if TrailerTextField contains any plate number from array of trailers
fileprivate func notContainsTrailerReg() -> Bool {
let filteredTrailers: [String] = trailersArray.map { $0.code.uppercased() }
return !filteredTrailers.contains(trailerTextField.text ?? String())
}
// Check if VehicleTextField is blank
fileprivate func vehicleIsBlank() -> Bool {
return vehicleTextField.text.isBlank
}
// Check if TrailerTextField is blank
fileprivate func trailerIsBlank() -> Bool {
return trailerTextField.text.isBlank
}
// When user press Next button
@IBAction func nextBtnTapped(_ sender: UIBarButtonItem) {
let selectInspection = storyboard?.instantiateViewController(withIdentifier: "SelectInspectionVC") as! SelectInspectionVC
selectInspection.receivedRegistrationNumber = RegistrationNumber(vehicleRegistrationNumber: vehicleTextField.text, trailerRegistrationNumber: trailerTextField.text)
let spinner = UIViewController.displaySpinner(onView: self.view)
if vehicleIsBlank() && trailerIsBlank() {
showAlertWithTitle(message: "Vehicle registration or/and Trailer Code required.")
}
else if (!vehicleIsBlank() && notContainsVehicleReg()) && (trailerIsBlank()){
showAlertWithTitle(title: "Vehicle not found", message: "Add vehicles via the PreDrive website to they are available for inspection.")
}
else if (!vehicleIsBlank() && notContainsVehicleReg()) && (!trailerIsBlank()){
showAlertWithTitle(title: "Vehicle not found", message: "Add vehicles via the PreDrive website to they are available for inspection.")
}
else if (!trailerIsBlank() && notContainsTrailerReg()) && (vehicleIsBlank()){
showAlertWithTitle(title: "Trailer not found", message: "Add trailers via the PreDrive website to they are available for inspection.")
}
else if (!trailerIsBlank() && notContainsTrailerReg()) && (!vehicleIsBlank()){
showAlertWithTitle(title: "Trailer not found", message: "Add trailers via the PreDrive website to they are available for inspection.")
}
else if (!vehicleIsBlank() && notContainsVehicleReg()) && (!trailerIsBlank() && notContainsTrailerReg()){
showAlertWithTitle(title: "Vehicle and Trailer not found", message: "Add vehicles and trailers via the PreDrive website to they are available for inspection.")
}
UIViewController.removeSpinner(spinner: spinner)
navigationController?.pushViewController(selectInspection, animated: true)
}
// Check if VehicleTextField or TrailerTextField contains spaces at beginning, then remove them
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let vehicleTextField = textField
let trailerTextField = textField
let vehicleString = "\(vehicleTextField.text ?? String())\(string )"
let trailerString = "\(trailerTextField.text ?? String())\(string )"
if (vehicleString.trimmingCharacters(in: .whitespaces).count == 0) || (trailerString.trimmingCharacters(in: .whitespaces).count == 0) {
return false
}
return true
}
显然是更好的方法,但我已经用笔和纸并考虑了所有可能性。