I am quite new to Swift and IOS, but I don't think that I should be stuck in the alert "popup". I can see the following but when pressing Restart, nothing happens.Alert popup
If I use a closure instead of nil to handler nothing happened.
I wrote the following in Swift5 using Xcode 10.3:
let alert = UIAlertController(title: title, message: message,
preferredStyle: .alert)
let action = UIAlertAction(title: "Restart", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
You need to dismiss the alert inside the completion handler of the ok action.
Related
This question already has answers here:
Present UIAlertController from AppDelegate [duplicate]
(7 answers)
Closed 3 years ago.
I am trying to make an alert on my app, but it keeps giving me warning like the below and it's not appearing
Warning: Attempt to present <UIAlertController: 0x..> on <xyz.VC1: 0x..> whose view is not in the window hierarchy!
the logic is like this :-
the IBAction in (VC1) calls a public function (X)
(X) the function do some operation and functions and based on it it's called the public function (Alert)
(Alert) the function should present an alert, but it gives me the previous warning.
NOTE: the alert works fine if I use it directly from the IBAction
present the alert :
func WAlert(){
// print("Wrong :("") // to be an alert
let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
//Cancel Action
}))
alert.addAction(UIAlertAction(title: "out",
style: UIAlertAction.Style.default,
handler: {(_: UIAlertAction!) in
//Sign out action
}))
present(alert, animated: true, completion: nil)
//self.present(alert, animated: true, completion: nil)
//VC1.present(alert, animated: true, completion: nil)
You probably need to have the function return the alert, rather than presenting it, and then present it (self.present) from VC1. As Teja Nandamuri mentions in comments, an alert must be presented from a visible UIViewController.
Revised based on comment:
You can generate the alert in a separate function, like your WAlert, but still present it in VC1, even in the IBAction. For instance, in the action you would have:
let alert = WAlert()
self.present(alert, animated: true)
You would need to change WAlert as follows:
func WAlert() -> UIAlertController {
let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
//Cancel Action
}))
alert.addAction(UIAlertAction(title: "out", style: UIAlertAction.Style.default, handler: {(_: UIAlertAction!) in
//Sign out action
}))
return alert
I have an alert and right after the alert is shown, I would like to present a different viewFinder. The doSomething() function is fired, "TEST" is printed, but the new viewfinder is not presented. What am I missing?
Alert
let alert = UIAlertController(title: "Sorry", message: "Booked out.",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style:
UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: self.doSomething)
content func doSomething()
print("TEST")
let details = storyboard?.instantiateViewController(withIdentifier: "ViewLimo2")
details?.transitioningDelegate = slideAnimatorRight
present(details!, animated: true, completion: nil)
The completion block on a view controller doesn't fire when the view controller is dismissed. It fires when the view controller finishes presenting (e.g. it has finished with viewDidAppear).
Honestly, I'd expect this to crash, since you're attempting to present while the alert is still presenting.
In any case, you need to wait until the dismissal of the UIAlertController before you try to present the next View Controller.
You could do it in the handler for the OK action:
let alert = UIAlertController(title: "Sorry", message: "Booked out.",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style:
UIAlertActionStyle.default, handler: doSomething))
self.present(alert, animated: true, completion: nil)
...
func doSomething(action:UIAlertAction) {
/// present the next VC here
}
I have a screen with a collection view of items. If the user has not selected anything, I want an alert to pop up, prompting them to choose something. If they have chosen something, I want an alert to pop up, asking if they are ready to move on? Below is my code for this:
if (isSelected) {
// create the alert
let alert = UIAlertController(title: "Create", message: "Make sure to select at least one item.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { action in
alert.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let alert2 = UIAlertController(title: "Move on", message: "Are you ready to move on?", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { action in
self.performSegue to next screen
}))
n.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: { action in
}))
}
The code seems to be fine but i get the following error:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
This seems like it should work pretty easily and a common thing to do, but there is no solution on line to this problem. Any help/guidance would be much appreciated.
you had missed to present the alert2
self.presentViewController(alert2, animated: true, completion: nil)
Add that and it will work fine.
This line is not required:
alert.dismissViewControllerAnimated(true, completion: nil)
Also add
self.presentViewController(alert, animated: true, completion: nil)
in else part.
I'm trying to make a UIAlertView on my Parse app but for some reason every time I run it, it crashes and I'm taken to ApplicationDelegate where I get a SIGABRT. Here is my alert code, I'm pretty sure I'm not doing anything wrong because it's worked before... Is it because I'm loading data into a tableview from Parse?
func displayAlert(title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
}))
presentViewController(alert, animated: true, completion: nil)
}
Not sure what your issue is. You could simplify the code as follows (Xcode 7b6) by eliminating the empty closure and shortening the arguments when possible. But I don't think this is your problem. Instead, you should show the code where you call this function.
var alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
I wrote the following function below to display alerts through my project. Every time i check for an error in a form i display the alert when the user clicks submit. I want to simply show the alert, but not unwind the pervious segue. I want to stay at the current screen and give the user the opportunity to complete the form. Right now when the user clicks submit, it displays the alert ..but when i click the ok button to dismiss the alert it immediately unwinds the segue to the previous screen.
I have included the UIAlertViewDelegate in the class.... any ideas why this might be happening?
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Never mind.
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
//comment out this line and it will still work. It will not segue back to the previous screen.
//self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}