如何检查多张图片 != nil
how to check for multiple images != nil
我正在尝试使用 if 语句和 &&
运算符检查是否有多个 UIImageView != nil
但问题是当用户选择 3 driverPImg.image != nil && passImg.image != nil && carImg.image != nil
下一个代码的任何照片时exited,它不会检查其余条件
这张图片显示了 3 张水平排列的图片,在 Btn 将颜色更改为红色之前,所有这些图片都应该被填充或不为零,但它应该发生的事情并没有发生
extension VC: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImg = info[.editedImage] as? UIImage {
SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
} else if let originalImg = info[.originalImage] as? UIImage {
SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
}
if let image = SelectedImg {
if pictureSelectionType == .profilePicture {
driverPImg.image = image
}else if pictureSelectionType == .idPicture{
passImg.image = image
}else if pictureSelectionType == .vhiclePicture{
carImg.image = image
}else {
backCarImg.image = image
}
if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
sendApplyBtn.backgroundColor = UIColor.delevareColor
}else {
sendApplyBtn.backgroundColor = UIColor.lightGray
}
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
我使用了您的代码并在 playground 上创建了一个片段。它似乎工作正常,我已经初始化了代码工作所需的变量。
var btnColor = UIColor.gray
var SelectedImg: UIImage? = UIImage.init(systemName: "house")
enum PictureSelectionType {
case profilePicture
case idPicture
case vhiclePicture
}
var pictureSelectionType: PictureSelectionType? = .idPicture
var driverPImg: UIImageView! = UIImageView()
var passImg: UIImageView! = UIImageView()
var carImg: UIImageView! = UIImageView()
var backCarImg: UIImageView! = UIImageView()
func imagePickerController() {
// if let editedImg = info[.editedImage] as? UIImage {
//
// SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
//
// } else if let originalImg = info[.originalImage] as? UIImage {
// SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
//
// }
if let image = SelectedImg {
if pictureSelectionType == .profilePicture {
let image = UIImage()
driverPImg.image = image
}else if pictureSelectionType == .idPicture{
passImg.image = image
}else if pictureSelectionType == .vhiclePicture{
carImg.image = image
}else {
backCarImg.image = image
}
if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
btnColor = UIColor.red
print("Red color")
}else {
btnColor = UIColor.lightGray
print("Gray color")
}
}
}
imagePickerController()
pictureSelectionType = .profilePicture
imagePickerController()
pictureSelectionType = .vhiclePicture
imagePickerController()
确保之前选择的图像已保存且未被取消,以便条件有效。
您可以使用占位符进行检查,这意味着如果您的占位符图像名为“placeHolder.png”
if driverPImg.image == UIImage(named: "placeHolder.png") && passImg.image == UIImage(named: "placeHolder.png") && carImg.image == UIImage(named: "placeHolder.png") {
sendApplyBtn.backgroundColor = UIColor.lightGray
}else {
sendApplyBtn.backgroundColor = UIColor.delevareColor
}
我正在尝试使用 if 语句和 &&
运算符检查是否有多个 UIImageView != nil
但问题是当用户选择 3 driverPImg.image != nil && passImg.image != nil && carImg.image != nil
下一个代码的任何照片时exited,它不会检查其余条件
这张图片显示了 3 张水平排列的图片,在 Btn 将颜色更改为红色之前,所有这些图片都应该被填充或不为零,但它应该发生的事情并没有发生
extension VC: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImg = info[.editedImage] as? UIImage {
SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
} else if let originalImg = info[.originalImage] as? UIImage {
SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
}
if let image = SelectedImg {
if pictureSelectionType == .profilePicture {
driverPImg.image = image
}else if pictureSelectionType == .idPicture{
passImg.image = image
}else if pictureSelectionType == .vhiclePicture{
carImg.image = image
}else {
backCarImg.image = image
}
if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
sendApplyBtn.backgroundColor = UIColor.delevareColor
}else {
sendApplyBtn.backgroundColor = UIColor.lightGray
}
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
我使用了您的代码并在 playground 上创建了一个片段。它似乎工作正常,我已经初始化了代码工作所需的变量。
var btnColor = UIColor.gray
var SelectedImg: UIImage? = UIImage.init(systemName: "house")
enum PictureSelectionType {
case profilePicture
case idPicture
case vhiclePicture
}
var pictureSelectionType: PictureSelectionType? = .idPicture
var driverPImg: UIImageView! = UIImageView()
var passImg: UIImageView! = UIImageView()
var carImg: UIImageView! = UIImageView()
var backCarImg: UIImageView! = UIImageView()
func imagePickerController() {
// if let editedImg = info[.editedImage] as? UIImage {
//
// SelectedImg = editedImg.withRenderingMode(.alwaysOriginal)
//
// } else if let originalImg = info[.originalImage] as? UIImage {
// SelectedImg = originalImg.withRenderingMode(.alwaysOriginal)
//
// }
if let image = SelectedImg {
if pictureSelectionType == .profilePicture {
let image = UIImage()
driverPImg.image = image
}else if pictureSelectionType == .idPicture{
passImg.image = image
}else if pictureSelectionType == .vhiclePicture{
carImg.image = image
}else {
backCarImg.image = image
}
if driverPImg.image != nil && passImg.image != nil && carImg.image != nil{
btnColor = UIColor.red
print("Red color")
}else {
btnColor = UIColor.lightGray
print("Gray color")
}
}
}
imagePickerController()
pictureSelectionType = .profilePicture
imagePickerController()
pictureSelectionType = .vhiclePicture
imagePickerController()
确保之前选择的图像已保存且未被取消,以便条件有效。
您可以使用占位符进行检查,这意味着如果您的占位符图像名为“placeHolder.png”
if driverPImg.image == UIImage(named: "placeHolder.png") && passImg.image == UIImage(named: "placeHolder.png") && carImg.image == UIImage(named: "placeHolder.png") {
sendApplyBtn.backgroundColor = UIColor.lightGray
}else {
sendApplyBtn.backgroundColor = UIColor.delevareColor
}