Search code examples
iosswiftuialertview

UIAlert view doesnt show up in Swift ?


I have written the following function and what I am trying to do is to notify the user by showing an alert.

func handleRecievedQR (QRCode : String) {
    let scannedCode = QRCode.split(separator: ",")
    let attendance = ScannedQRCode(issuer: String(scannedCode[0]),
                                   eventName:String(scannedCode[1]) ,
                                   eventCode: String(scannedCode[2]),
                                   issuedYear: Int(scannedCode[3])!,
                                   issuedMonth: Int(scannedCode[4])!,
                                   issuedDay: Int(scannedCode[5])!,
                                   issuedHour: Int(scannedCode[6])!,
                                   issuedMin: Int(scannedCode[7])!,
                                   lat: Double(scannedCode[8])!,
                                   long: Double(scannedCode[9])!
    )

    if (attendance.validateTime() ){

        if (attendance.validateLocatoin() ){
            eventName.text = attendance.getEventName()
            eventCode.text = attendance.getEventCode()
            fullName.placeholder = "Enter your full name here"
            SignBtn.titleLabel?.text = "Sign the attendece"


        }else {
            //The problem is here. For debuggin I have used a print statement to make sure that the program has reached that point
            print("You are not present here ")
            let alert = UIAlertController(title: "Message", message: "It's detected that you are not actually in the event", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Note", style: .default, handler: { action in
                switch action.style{
                case .default:
                    print("HI")
             case .cancel:
                    return
                case .destructive:
                    return
                }}))


        present(alert, animated: true, completion: nil)

        }
    }else{
        print("Too late to scan the code")

    }

}

this function is called in the viewDidLoad but I cant manage to show any AlertView. Any idea where am I making my mistake and how can I fix it ?


Solution

  • It will not show up in viewDidLoad(). You call the function from viewDidAppear()