I show a modal viewcontroller on which the user can decide to edit oder delete the presented car.
If the user wants to delete this car I present an UIAlertController with alert style to ask if he really wants to delete this car. Everything works fine. But after the user chooses "Yes" I am still in the modal viewcontroller.
How can I dismiss the modal view after the deletion?
I tried Following Code
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
and
self.navigationController?.popViewControllerAnimated(true)
in the closure of the ok Action but both didn't worked for me. :(
There is no reason to write the code in
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
The above code is used to the work in main thread. But here you are already on the main thread.
The issue here is you are calling
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
Instead of it just write
self.dismissViewControllerAnimated(true, completion: nil)
Because you are presenting AlertController on self controller so the only one who can dismiss it is self
Put your code inside the
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
like that :
func showDeleteWarningAndDeleteEntity() {
dispatch_async(dispatch_get_main_queue(), {
let deleteController = UIAlertController(title: "Delete car", message: "Are you sure?", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {
(action) in
}
let okACtion = UIAlertAction(title: "Yes", style: .Destructive) {
(action) in
//some network stuff happens here...
self.dismissViewControllerAnimated(true, completion: nil)
}
deleteController.addAction(okACtion)
deleteController.addAction(cancelAction)
self.presentViewController(deleteController, animated: true, completion: nil)
})
}
Hope this will works for you
Related
I'm trying to dismiss the current view controller in the completion handler of a UIAlertAction, but it is not dismissing. I have written the following code (The loading indicator is simply a loading alert controller that I dismiss when the data was successfully uploaded):
loadingIndicator.dismiss(animated: true) {
let success = UIAlertController(title: "Successfully Uploaded", message: "", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: { _ in
print("Ok selected") //this is working correctly
self.dismiss(animated: true, completion: nil) //this is not
})
success.addAction(ok)
self.present(success, animated: true, completion: nil)
}
However, after clicking on "Ok" in the alert, "Ok selected" is printed but the view controller is not dismissed. Nothing else shows up in the debugger.
Try dismissing it on Main thread and also check if ViewController is presented or pushed in navigation hierarchy.
loadingIndicator.dismiss(animated: true) {
let success = UIAlertController(title: "Successfully Uploaded", message: "", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: { _ in
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
// If pushed use PopViewController on navigation
// self.navigationController?.popViewController(animated: true)
}
})
success.addAction(ok)
self.present(success, animated: true, completion: nil)
}
To check if ViewController is being presented or not use self.isBeingPresented property.
I am trying to completely deallocate my view controller from memory. After hours of testing, I've finally narrowed it down to a UIAlertController staying in memory which keeps my view controller from deallocating.
#objc func logout_click() {
let alert = UIAlertController(title: "Confirmation", message: "Are you sure you want to log out?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "YES", style: .default, handler: { _ in
// 'YES' button action
do {
try Auth.auth().signOut()
self.popInit()
} catch {
print(error)
}
}))
alert.addAction(UIAlertAction(title: "NO", style: .default, handler: { _ in
// 'NO' button action
alert.dismiss(animated: true)
}))
self.present(alert, animated: true)
}
func popInit() {
//Go back to init screen
self.navigationController?.popToRootViewController(animated: true)
}
As long as this alert doesn't show, I can use popInit() and my view controller deallocates just fine, but after this alert shows up, even after dismissing, the view controller will not deallocate. I am not referencing any variables outside the scope of this function, so why does this not allow me to deallocate? What do I need to do to allow my view controller to deallocate?
Have the YES action handler declare [weak self] and call self?.popInit().
Also, as suggested in a comment, you can replace the NO handler with nil.
I'm having a homeView which is the initial view to appear when app launches. In its viewWillAppear() method I check if the user is logged in. If not, he's redirected to login page and if he's not yet registered he'll be directed to sign-up page. I get an error when user clicks the sign-up button. The signUPView appears but it gives a warning that Attempt to present loginViewController on signupViewController whose view is not in the window hierarchy. I have show detail segues between sign-up and login views.
I'm performing the segue after uialert is presented.
I got solutions to put the code in viewDidAppear or ViewWillAppear method but I'm presenting the viewControllers upon button press. So I can't use these methods.
I have searched for a lot of solutions but unable to get the right one.
Please help!
Thank you.
segue triggering code from sign-up :
dispatch_async(dispatch_get_main_queue()) {
if self.registerSuccess == true {
let alert = UIAlertController(title: "Registered!", message: "You have registered successfully!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
self.nameTextField.text = ""
self.emailTextField.text = ""
self.passwordTextField.text = ""
self.performSegueWithIdentifier("loginSegue", sender: self)
}))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Registration Failed!", message: "Please try again", 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)
}
}
screenshot of my storyboard
#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.
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)