I’m writing an iOS app in Swift. When the app starts, I run some internal data integrity checks. If they fail, I want to display something. I tried to put a modal UIAlertController into the viewDidAppear on my initial view, but got an NSException,
Does anyone have any suggestions of how to display an error/warning/alert at the start of the app.
Can you share the details of the NSException? It is likely that it was unrelated to it being in viewDidAppear, but will need more details to help you track it down.
Here’s the code:
override func viewDidAppear(_ animated: Bool) {
NSLog("View Did Appear")
for (sku,_) in priceList {
if productTallySequence.contains(sku) {
}
else {
NSLog("Data Integrity Failure")
let ac = UIAlertController(title: "Data Integrity Failure",
message: "\(sku) in price list is missing from the tally report",
preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
ac.addAction(cancelAction)
ac.present(ac, animated: true, completion: nil)
print("\(sku) is in the price list but is missing from the tally report")
}
}
}
Here’s the exception:
2017-04-02 21:40:14.815 WWTally1[15029:653955] View Did Appear
2017-04-02 21:40:14.816 WWTally1[15029:653955] Data Integrity Failure
2017-04-02 21:40:14.852 WWTally1[15029:653955] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Application tried to present modal view controller on itself. Presenting controller is <UIAlertController: 0x7fa25dc30950>.’
I don’t get the exception if I comment out the ac.preset( ) call.
Thanks
The issue is calling ac.present() instead of present(). You want the ViewController to present the alert controller, but the current code is telling the alert controller to present the alert controller.
Also, since you are doing this in a loop, you will should consider a return statement after present(ac…) to avoid presenting the user with a bunch of alerts.
Hope this helps
1 Like
Thanks. That’s what the code snippet I’d learned from originally had. But it wouldn’t compile with present(… I thought it was a typo so I added ac… I’ll take the ac. off and look for another solution to the compile error.
If you want to share the compiler error you receive when you use present(ac…) I’d be happy to take a look at it and help you identify what went wrong the first time.
It’s good now! No compile error and it works as expected. I have no clue what might have been causing the compile error. Thanks for your help.