Search code examples
swift3uialertviewcollectionview

How to show Alert if CollectionView cell is not selected in Swift 3?


I am working on a Doctor Appointment App. I have a collection view in which cells are time slots. User can select time slot based on date of his favour.I placed a button under the collection view and after clicking that button user moves to next viewController. I want to show alert on clicking a button if user doesn't select any time slot. I tried a lot but not finding a solution. Thanks in advance.... check the screenshot of my view. Hoping someone would help me....


Solution

  • proceed as following:

    @IBAction func buttonPressed(_ sender: Any) {
                if timeSlotsCollectionView.indexPathsForSelectedItems == [] {
                    let alert = UIAlertController(title: "DID NOT SELECT A TIME SLOT", message: "Please select a time slot!", preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
    }
    

    Good luck with your App