#IBAction func addButton(sender: AnyObject) {
let alert = UIAlertController(title: "New Exercise Added", message: "\(name)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
self.dismissViewControllerAnimated(true, completion: {})
}
Within the IB action function of a button I have an alert, followed by some code to change to a different ViewController.
The program crashes upon reaching these lines of code after the alert:
2016-01-04 17:48:27.147 FitnessApp[60584:4080964] popToViewController:transition: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.
How do I run the code to change ViewController after the transition is done?
Your biggest issue is that you don't do anything with the alert button's handler. Instead, you immediately try to do the pop and dismiss after presenting the alert.
Move the code to pop the controller into the Ok button's alert handler.
#IBAction func addButton(sender: AnyObject) {
let alert = UIAlertController(title: "New Exercise Added", message: "\(name)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok!!", style: UIAlertActionStyle.Default, handler: {
self.navigationController?.popToRootViewControllerAnimated(true)
// You only need the pop
//self.dismissViewControllerAnimated(true, completion: {})
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Note: I'm not fluent in Swift so the syntax could be off a little.
Related
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
}
Today I'd like to try to make a part of an application that after show an alert perform a segue ONLY if the user press on the "Yes" alert's button.
To explain me better I'd like that the application will show an aller that say "Are you sure to return to Home?" and it will have two Botton: "Yes" and "No". If the user press no nothing happen, if the user press yes the application perform a segue. The problem is that I don't know how to do it.
I try to write some code line but it doesn't work.
func Alert (TITLE: String, MESSAGE: String) -> Bool()
{
var X = false
let Alert = UIAlertController(title: TITLE, message: MESSAGE, preferredStyle: UIAlertControllerStyle.alert)
Alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: { (Action) in
X = true
Alert.dismiss(animated: true, completion: nil)
}))
self.present(Alert, animated: true, completion: nil)
Alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
self.present(Alert, animated: true, completion: nil)
return X
}
#IBAction func ButtonAct(_ sender: Any) //This happen if you click the botton on the screen of the iPhone
{
if Alert (TITLE: "Return to Home", MESSAGE: "Are you sure to return Home?")
{performSegue(withIdentifier: "Segue", sender: self)}
}
Thank you for you Help
There are several problems with your code:
No part of your code is calling the #IBAction method, that is why the segue is not being performed. The alert controller only provides you with completion handlers; there is no target/action mechanism like with UIButton.
The boolean return value is determined inside the action's completion handler, asynchronously. Your function returns right away the value false, before the user has had a chance to chose. Things happen in this order:
Call alert()
Function presents UIAlertController
Function returns false
(very many milliseconds pass)
User taps "YES"
Completion handler is executes and x is set to true, but the alert() method has already returned false.
You are presenting the alert twice, once after adding each action. You need to add both actions and then present it only once.
Please use standard capitalization in your code.
Try something like this:
func alert (title: String, message: String, completion: ((Bool) -> Void)) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in
alertController.dismiss(animated: true, completion: nil)
completion(true) // true signals "YES"
}))
alertController.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: { (action) in
alertController.dismiss(animated: true, completion: nil)
completion(false) // false singals "NO"
}))
self.present(alertController, animated: true, completion: nil)
}
...and call it like this:
alert(title: "Hi", message: "Want to proceed?", completion: { result in
if result {
self.performSegue(withIdentifier: "Segue", sender: self)
}
})
I have this function and I use it on almost all viewController´s that I have, for some reason went I push "ok" to dismiss the pop up, it switches to another viewController.
this is my function...
extension UIViewController
{
func displayAlert(title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler:
{ (action) -> Void in
self .dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(alert, animated: true, completion: nil)
}
}
any help? I'm using Xcode 7.0 beta 6 with swift 2
You don't need to dismiss the alert controller yourself. That happens automatically with your alert action. You're simply providing a closure that gets called when that "OK" action gets triggered. Just remove this line:
self.dismissViewControllerAnimated(true, completion: nil)
I have a view controller which is modally presented and when i run UIAlertController, keep getting a error message . how can i work around this.
the alert is trigged from a button which checks a text field is empty or not.
#IBAction func registerAction(sender: AnyObject) {
if(userEmail.isEmpty){
alertMessage("Fields Are Empty")
}
}
func alertMessage(userMessage:String){
var myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
}
Error when i run the alert
Warning: Attempt to present <UIAlertController: 0x7f9bbb9060d0> on <AlertApp.CustomSignupVC: 0x7f9bb94accb0> which is already presenting <UIAlertController: 0x7f9bbb926660>
Try to add it in main operation queue.
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(myAlert, animated: true, completion: nil)
}
Check this relative question : Presenting a view controller modally from an action sheet's delegate in iOS8 - iOS10
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)
}