Perform segue after UIAlertController is dismissed
The problem is in this code:
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
self.performSegue(withIdentifier: "segue", sender: nil)
})
The handler:
is not a completion handler. It runs before the alert is (automatically) dismissed. Thus, you are starting the segue while the alert is still present.
If you prefer not to use delay
(though I see nothing wrong with that approach), what I would try is this:
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
CATransaction.setCompletionBlock({
self.performSegue(withIdentifier: "segue", sender: nil)
})
})
Alert Blocking Segue-iOS
Add a completion handler to your alert's dismiss button like this
let alert = UIAlertController(title: "Alert", message: "Content Here", preferredStyle: .alert)
// add textfield or whatever you need
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
self.performSegue(withIdentifier: "secondVC", sender: self)
}))
present(alert, animated: true)
The completion handler will be called when the user presses the "OK" button on the alert.
Show UIAlert before unwinding segue
Instead of perform the segue directly you need to show your UIAlertViewController
first and according to the user response execute your segue
or not
@IBAction func showAlertViewController(){
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let replyAction = UIAlertAction(title: "Delete Draft", style: .destructive, handler: nil)
let replyAllAction = UIAlertAction(title: "Save Draft", style: .default) { (action) in
//Do whatever you need here
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
self.performSegue(withIdentifier: "cancelDraft", sender: action) //executing the segue on cancel
}
alertController.addAction(replyAllAction)
alertController.addAction(replyAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
After this you only need to change the unwind segue action to execute this method, and your segue will be executed if you press cancel in the UIAlertViewController
via self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)
UIAlertController keeps segue from performing (window is not equal to view’s window“)
You can't present an alert and do a segue at the same time use DispatchQueue.main.asyncAfter
to mook a waiting
private func setUpDialog() {
alertMessage = "Logging in"
alert = UIAlertController(title: "Please wait", message: alertMessage, preferredStyle: UIAlertController.Style.alert)
self.present(alert, animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.alert.dismiss(animated: true, completion: {
self.performSegue(withIdentifier: "segue12", sender: self)
})
}
}
How do I dismiss a UIAlertController when the return key is tapped in Swift?
Instead of calling dismiss
on myAlertController
, try calling dismiss
on your AlbumViewController
. I've edited your textFieldShouldReturn
function below:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let TFNameToSave = textField.text
self.save(name: TFNameToSave!)
self.albumCollectionView.reloadData()
textField.resignFirstResponder()
// ViewController should dismiss the alert controller
dismiss(animated: true) {
self.performSegue(withIdentifier: "segueForTF", sender: self)
print("Album name has been inputted; return button tapped.")
}
return true
}
From the docs:
The presenting view controller is responsible for dismissing the view
controller it presented.
Here, the presenting view controller should be the AlbumViewController
and the presented is myAlertController
.
Hope that works!
Present a ViewController, dismiss it, and then segue into another View
Perform segue on completion of dismiss of UIImagePickerController
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// your code
self.dismissViewControllerAnimated(true, completion: {
self.performSegueWithIdentifier("SegueIdentifier", sender: self)
})
}
Note: With sender I have passed current Controller object you can any object that you want.
Related Topics
Apple Watch and iPhone Are Not Connected When The App in Phone Goes to Background
Realm: Predicate Returning Lazyfiltercollection - How to Convert to Results<T>
Swift Combine How Set<Anycancellable> Works
Destructuring Tuple of Tuple in Closure
How to Put View on Top of All Other Views in Swiftui
Viewwilllayoutsubviews in Swift
Scrolltoitem at Indexpath at .Top Hides Cell Under Header When Sectionheaderspintovisiblebounds
Protocol Associated Type Typealias Assignment Compile Error
Tableview.Cellforrowatindexpath(Indexpath) Return Nil
Easiest Way to Truncate Float to 2 Decimal Places
Nil Cannot Be Assigned to Type Avcapturedeviceinput
Difference Between Firinstanceid.Instanceid().Token() and Messaging.Messaging().Fcmtoken
How to Test If an Instance Is a Specific Class or Type in Swift
Swift iOS14 Datepicker Text Alignment
Preferredstatusbarupdateanimation Being Ignored
Decrypted String Always Returning Null
Passing in Variable Number of Args from One Function to Another in Swift