I'm trying to fire an alert that asks if you want to save or delete a draft after pressing cancel. I'm quite close, but I can't seem to get it right.
I'm unwinding from 'ReplyMailViewController'(ViewController A) to 'MailContentViewController'(ViewController B).
I added the following code in ViewController A to show the alert and 'hold' the segue perform:
override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool {
if let ident = identifier {
if ident == "cancelDraft" {
let saveDraftActionHandler = { (action:UIAlertAction!) -> Void in
NSLog("EXIT")
}
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let deleteDraftAction = UIAlertAction(title: "Delete Draft", style: .destructive, handler: nil)
alertController.addAction(deleteDraftAction)
let saveDraftAction = UIAlertAction(title: "Save Draft", style: .default, handler: saveDraftActionHandler)
alertController.addAction(saveDraftAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
return false
}
}
return true
}
The segue holds with this code, but the issue is that I can't figure out how to continue the unwind segue after pressing 'Save Draft' for example.
I also have an unwind function in View Controller B, but I can't seem to figure out how I can use this one for this task:
#IBAction func cancelToMailContentViewController(_ segue: UIStoryboardSegue) {
}
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>)
First, make the alert with two options:
class ViewController: UIViewController {
#IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "UIAlertController", message: "Save this work?", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Hell Yeah", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Hell No", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
After this, you have to make the segue and then name it (also connect it by control dragging from the view controller yellow icon to the other view controller):
After that put this your code to execute the segue:
self.performSegue(withIdentifier: ":)", sender: self)
After that you are going to execute the segue when the user responds to the alert:
if buttonTitle == "Hell Yeah" {
elf.performSegue(withIdentifier: ":)", sender: self)
}
so, in the end, your code should look like this:
class ViewController: UIViewController {
#IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "UIAlertController", message: "Save this work?", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Hell Yeah", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Hell No", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
if buttonTitle == "Hell Yeah" {
self.performSegue(withIdentifier: ":)", sender: self)
}
}
}
Related
I have two buttons, one that shows the segmented control and one that tries to hide it. The problem is when I click the one to show it, it works. However, when I click the one to hide it, it doesn't work. Here is my code:
let delayHide = UIAlertAction(title: "Hide Delay", style: .default) { (action) in
self.segmentedHidden = 1
self.setupSegmented()
}
let delayShow = UIAlertAction(title: "Show Delay", style: .default) { (action) in
self.segmentedHidden = 0
self.setupSegmented()
}
Here is also the code for when I try to hide it:
if (segmentedHidden == 0) {
segmentedControl.isHidden = false
} else {
segmentedControl.isHidden = true
}
Where did I go wrong?
Use following code:
#IBAction func buttonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "Alert", message: "Segment", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Show", style: .default, handler: { (alertAction) in
self.showHideSegmentControl(isHidden: false)
}))
alert.addAction(UIAlertAction(title: "hide", style: .default, handler: { (alertAction) in
self.showHideSegmentControl(isHidden: true)
}))
self.present(alert, animated: true, completion: nil)
}
func showHideSegmentControl(isHidden: Bool) {
segmentedControl.isHidden = isHidden
}
I create an alert with UIAlertController and add some action in it. But when I touch button on it and, nothing changed. (Include cancel button) so I can only restart this application can solve this question.
let alert = UIAlertController(title: “my alert”, message: “some message...”, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: “button1”, style: .default, handler: { (_) in
print(“button 1 up inside”)
}))
alert.addAction(UIAlertAction(title: “button2”, style: .default, handler: { (_) in
print(“button 2 up inside”)
}))
alert.addAction(UIAlertAction(title: “cancel”, style: .cancel, handler: { (_) in
}))
self.present(alert, animated: true, completion: nil)
Xcode 10 built.
I had a similar problem in an app using the Sprite Kit. Here was the solution:
let connectActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
connectActionSheet.addAction(UIAlertAction(title: "Button1", style: .default, handler: { (action:UIAlertAction) in
//Code if this button is pressed
}))
connectActionSheet.addAction(UIAlertAction(title: "Button2", style: .default, handler: { (action:UIAlertAction) in
//Code if button2 is pressed
}))
connectActionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
let vc = self.view?.window?.rootViewController
if vc?.presentedViewController == nil {
vc?.present(connectActionSheet, animated: true, completion: nil)
}
The last 4 lines were what made it worked. I hope this helps you.
Thanks a lot! My view controller can’t close this alert because these code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
}
I am trying to create a UIAlertController with two options 'Cancel' and 'Log out'. I want the 'Cancel' button to cancel the alert and the 'Log out' button to perform the segue associated with it, which i have set up in the storyboard.
My code is;
class HomeVC: UIViewController {
#IBAction func SignOutBtn(sender: UIButton) {
let alertController = UIAlertController(title: "Alert",
message: "Are you sure you want to log out?",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title:"Cancel",
style: .Cancel) { (action) -> Void in
print("You selected the Cancel action.")
}
let submitAction = UIAlertAction(title:"Log out",
style: .Default) { (action) -> Void in
print("You selected the submit action.")
self.presentedViewController
}
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Well it seems your'e missing the actions you want to perform inside the blocks.
(also, you may want to dismiss the alert controller, inside the blocks as well.)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
print("You selected the Cancel action.")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in
print("You selected the submit action.")
alertController.dismissViewControllerAnimated(true, completion: { () -> Void in
// Perform your custom segue action you need.
})
})
If you have set up a segue from one view to another, in your button handler for log out then you can call self.performSegueWithIdentifier("storyboadIdentifier") which will call the prepareForSeguemethod so you can modify or pass info along the segue if need be.
#pbush25
class HomeVC: UIViewController {
#IBAction func SignOutBtn(sender: UIButton) {
let alertController = UIAlertController(title: "Alert",
message: "Are you sure you want to log out?",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
print("You selected the Cancel action.")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in
print("You selected the submit action.")
alertController.dismissViewControllerAnimated(true, completion: { () -> Void in
self.performSegueWithIdentifier("segue", sender: nil)
})
})
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
I am using an alert to perform a Segue to another view.
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
performSegueWithIdentifier("proceed", sender: self)
})
I just can't figure out why I get the error:
Extra argument 'sender' in call
The sender should be just self, right?
This error is driving me crazy for hours now, I can't figure out what's wrong!
Any help is appreciated! Thanks!
Try this code :
override func viewDidLoad() {
super.viewDidLoad()
self.showAlert() //Calling function
}
func showAlert()
{
let alertController = UIAlertController(title: "Download Complete", message: "The list of user IDs has been downloaded. Proceed to know who viewed your profile.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
self.performSegueWithIdentifier("proceed", sender: self)
})
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion:nil)
}
So this is my full code:
let alertController = UIAlertController(title: "Download Complete", message: "The list of user IDs has been downloaded. Proceed to know who viewed your profile.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Proceed", style: .Default, handler: { (UIAlertAction) -> Void in
performSegueWithIdentifier("proceed", sender: nil)
})
override func viewDidLoad() {
super.viewDidLoad()
self.alertController.addAction(self.defaultAction)
self.presentViewController(self.alertController, animated: true, completion: nil)
}
try this
let alert = UIAlertView()
alert.title = "Download Complete"
alert.message = "he list of user IDs has been downloaded. Proceed to know who viewed your profile."
alert.addButtonWithTitle("ok")
alert.show()
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
performSegueWithIdentifier("proceed", sender: nil)
}
alertController.addAction(okAction)
The UIAlertViewController shows all my text but on clicking doesn't switch to the other View. Here's the code:
let alertController = UIAlertController(title: "Risposta Esatta", message:"", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Avanti", style:UIAlertActionStyle.Default,handler: {(UIAlertAction) in
let secondViewController:SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
}))
This just worked for me:
#IBAction func choiceBtn(sender: UIButton) {
let alertController = UIAlertController(title: "Hello!", message: "This is Alert sample.", preferredStyle: .Alert)
let otherAction = UIAlertAction(title: "OK", style: .Default) {
action in println("pushed OK!")
}
let cancelAction = UIAlertAction(title: "CANCEL", style: .Cancel) {
action in self.performSegueWithIdentifier("VC2", sender: self)
}
alertController.addAction(otherAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}
I created a modal presented segue from VC1 to VC2 and also named the identifier VC2. This code I adapted from the GitHub UIAlertController example here:
https://github.com/eversense/Swift-UIAlertController-Example
There was no segue to another ViewController, so I added that to show you.