警报视图未显示在 Swift 中?
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)
我编写了以下函数,我想做的是通过显示警报来通知用户。
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)