UIAlert视图没有出现在Swift中?

时间:2018-03-31 14:15:51

标签: ios swift uialertview

我写了以下功能,我想做的是通过显示提醒来通知用户。

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

    }

}

viewDidLoad中调用此函数,但我无法显示任何AlertView。我知道我在哪里弄错了,我该怎么办呢?

2 个答案:

答案 0 :(得分:2)

它不会显示在viewDidLoad()中。您可以从viewDidAppear()

调用该函数

答案 1 :(得分:1)

您永远不会出现提醒视图。您应该将显示提醒的代码移至viewDidAppear(),如Martin所述,然后您需要添加一行self.present(alert, animated: true, completion: nil)