gpt4 book ai didi

ios - 如何检查两个可选的 UITextFields

转载 作者:行者123 更新时间:2023-11-28 05:42:30 24 4
gpt4 key购买 nike

我有 2 个 UITextFields:一个是 VehiclePlateNumber,另一个是 TrailerPlateNumber。它们都是可选的。但是,如果它们都丢失了,那么当我单击 NEXT 按钮时,我将显示一条消息。问题是我无法在只有 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
}
}

如果你想帮助我,谢谢。

最佳答案

只需将您的 nextBtnTapped(:UIBarButton) 函数更改为此即可。这将检查两个 textFields 是否为空,如果其中一个或两个都正确填充,它将转到下一个 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)
}
}

关于ios - 如何检查两个可选的 UITextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56114896/

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