[英]UIAlert view doesnt show up in Swift ?
我已经编写了以下函数,我想做的是通过显示警报来通知用户。
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。 知道我在哪里犯错误以及如何解决吗?
它不会显示在viewDidLoad()
。 您从viewDidAppear()
调用函数
您永远不会显示警报视图。 如Martin所述,您应该将显示警报的代码移至viewDidAppear()
,然后需要添加一行self.present(alert, animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.