Search code examples
iosswiftxcodeif-statementuiimageview

how to check for multiple images != nil


I am trying to check if multiple UIImageView != nil using if statement and && operator but the problem is that when the user pick any photo of the 3 driverPImg.image != nil && passImg.image != nil && carImg.image != nil the next code exuted and it will not check for the rest conditions

this image shows 3 images line up Horizontally all those images should be filled or not nil before the Btn change color to red but what it suppose to happen didn't happen

this image shows 3 images line up Horizontally all those images should be filled or not nil before the Btn change color to red but what it suppose to happen didn't happen

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)
    }
    
}

Solution

  • you can check it by using your placeholder, which means if your placeholder image called "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
                }