Good day,
This is my code:
#IBAction func logOutAction(sender: AnyObject) {
try! FIRAuth.auth()!.signOut()
let alertController = UIAlertController(title: "Sure?", message: "Are you sure you want to log out?", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: nil)
alertController.addAction(defaultAction)
let defaultAction2 = UIAlertAction(title: "No", style: .Cancel, handler: nil)
alertController.addAction(defaultAction2)
self.presentViewController(alertController, animated: true, completion: nil)
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view
self.presentViewController(VC, animated: true, completion: nil)
}
As you can see I want to log out the user when pressing the logout button. When the user clicks logout a UIalert popsup asking if they are sure they want to log out, but when I click okay it doesn't switch back to the loginscree ViewController.
Can anyone help me?
If the last two lines represent the view that you want to present, then you have to execute this code inside the handler of the "Yes" button action:
let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: { _ in
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view
self.presentViewController(VC, animated: true, completion: nil)
})
alertController.addAction(defaultAction)
You need to logout after somebody click "Yes"
let yesActionHandler = { (action:UIAlertAction!) -> Void in
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view
self.presentViewController(VC, animated: true, completion: nil)
}
let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: yesActionHandler)
alertController.addAction(defaultAction)
Related
How to show or link to new ViewController when push button on alert?
This is my code
let alert = UIAlertController(title: validateQRObj.responseDescription, message: validateQRObj.productName, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ProductDetialViewController")
self.present(viewController!, animated: true, completion: nil)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
Control drag from View Controller 1 (The yellow dot) to any where on View Controller 2 and then click on the Segue. Show the Attributes inspector and Under Storyboard Segue identifier name the identifier VC2
If this is the answer you were looking for don't forget to except the answer.
func alert(){
let alertController = UIAlertController(title: "Open View Controller. ", message: "Press Ok to open View Controller number 2.", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) -> Void in
//The (withIdentifier: "VC2") is the Storyboard Segue identifier.
self.performSegue(withIdentifier: "VC2", sender: self)
})
alertController.addAction(ok)
self.present(alertController, animated: true, completion: nil)
}
For me it doesn't work. I made :
func alert(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let twop = storyboard.instantiateViewController(withIdentifier: "Accueil") as! Accueil
let alertController = UIAlertController(title: "Open new View !", message: "Clic to ok", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) -> Void in
self.navigationController?.show(twop, sender: self)
})
alertController.addAction(ok)
self.present(alertController, animated: true, completion: nil)
}
and now it's working !!
I'm quite new to Swift (and coding in general) and to this website.
I'm having a little bit of an issue now. In my app, I have an alert when a timer reaches 0. In that alert, there are 2 buttons. One says "Share" and the other says "Continue". I want to make it such that when a user taps "Continue", the next Storyboard will be shown to them. As of now, whatever button I press will dismiss the alert, but stay on the same Storyboard. (It also prints to the console which button I pressed, but of course that's just for me to see).
How do I go about doing this? Here is my code, in case anyone wants to know.
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button one")
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button two")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
presentViewController(alert, animated: true, completion:nil)
Try with this:
// Create the alert controller
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
//do whatever you want here
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
If you haven't already set your ViewControllerIdentifier:
You need to set the Identifier value that you can find on the right column
Here is the code that might help you. When you create a AlertController with actions, you can provide the actions and style of the button when you define them. In code below action is in the closure (Block) and style is defined as .Default or .Cancel
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
// Action when you press button goes here
print("Here you show next storyboard item.")
// Code to push new ViewController. For example :
self.navigationController?.pushViewController(newVCInstance, animated: true)
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
print("Here you share things.")
// Code to share things.
}
let thirdAction = UIAlertAction(title: "Cancel", style: . Cancel) { (alert: UIAlertAction!) -> Void in
print("Just dismissing the Alert Controller.")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
alert.addAction(thirdAction)
presentViewController(alert, animated: true, completion:nil)
I want to display a viewcontroller called InViewController, when the "OK" from the add.alertAction is pressed.
if ((user) != nil) {
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewControllerIdentifier")
self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
You can add a completionHandler to the UIAlertAction when you add it to do it what you want, like in the following way:
if ((user) != nil) {
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default, handler: { _ -> Void in
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerA") as! ViewControllerA
self.presentViewController(nextViewController, animated: true, completion: nil)
})
alert.addAction(OKAction)
self.presentViewController(alert, animated: true){}
}
To set the StoryboardID you can use Interface Builder in the Identity Inspector, see the following picture:
I put everything in the above code referencing ViewControllerA, you have to set the name of your UIViewController according what you want.
EDIT:
You are pointing to a UIView or some other object on the StoryBoard. Press the yellow indicator on top of the other objects which is your UIViewController, like in the following picture:
I hope this help you.
Here's how you can do that ,
I'm just updating the good work of Victor Sigler
you follow his answer with this little update ..
private func alertUser( alertTitle title: String, alertMessage message: String )
{
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let actionTaken = UIAlertAction(title: "Success", style: .default) { (hand) in
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "IntroPage") as? StarterViewController
self.present(destinationVC!, animated: true, completion: nil)
}
alert.addAction(actionTaken)
self.present(alert, animated: true) {}
}
I am quite new to Swift development and I tried referring to the Swift UIAlertController API but couldn't figure out how to navigate to another UIViewController after clicking a button on the UIAlertController.
I would appreciate any pointers, help or a solution to this problem. My code snippet is given below -
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
let secondVC = ViewController(nibName: "ViewController", bundle: nil)
let navController = UINavigationController(rootViewController: secondVC)
self.presentViewController(navController, animated: true, completion: nil)
}
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
What I am trying to do here is when a button is clicked the disclaimer AlertController is displayed. If "Decline" button is selected, cancel action is performed and if "Accept" is selected the app should navigate to a Navigation Controller which then allows me to navigate to other ViewControllers using a menu in the application.
Earlier I used the story board to link a button to the NavigationController to traverse to the ViewController I desired. Now I want to do the same programmatically for the AlertController "Accept" Button.
Thank you in advance.
You need to implement the handler block to perform code when an action is selected :
#IBActionfunc showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
let secondVC = SecondViewController(nibName: "SecondView", bundle: nil)
let navController = UINavigationController(rootViewController: secondVC)
self.presentViewController(navController, animated: true, completion: nil)
}
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
Basically to link the UIAlertViewController button to a UINavigationController's UIViewController we would need to create a manual segue between the UIViewController which has the UIAlertViewController to the UINavigationController.
Please refer to this screen shot to see how to do the above -
Then select the link between the UIViewController and the UINavigationController. Go to the left sidebar and select the Attributes inspector and name the identifier.
Now write the following in your code -
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier (name)
}
alertController.addAction(declineAction)
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
That's the use of the handler block. You should make your desired action in this block.
Edit: Code
let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{ action in
//Write your code here
})
You can use this link as a reference: http://www.appcoda.com/uialertcontroller-swift-closures-enum/
In my code I have an action sheet that allows a user to either log-in or sign-up. When the user presses either one of these I want to define a handler that will segue them to the login/signup view controller. I'm very new to this so I'm not quite sure what I'm doing wrong but in my code I get an error that the types are not convertible. The code looks like this:
let actionSheet = UIAlertController(title: "Do you have an account?", message: "Please Log-In or Sign-Up", preferredStyle: .ActionSheet)
if let presentationController = actionSheet.popoverPresentationController {
presentationController.sourceView = sender as UIView
presentationController.sourceRect = sender.bounds
}
let dismissHandler = {
(action: UIAlertAction!) in
self.dismissViewControllerAnimated(true, completion: nil)
}
let signupHandler = ({
(ViewController: UIViewController!) in
let signupVC: nextViewController = segue.signupVC as nextViewController
})
let loginHandler = ({
(ViewController: UIViewController!) in
let loginVC: nextViewController = segue.loginVC as nextViewController
})
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: dismissHandler))
actionSheet.addAction(UIAlertAction(title: "Log-In", style: .Default, handler: dismissHandler))
actionSheet.addAction(UIAlertAction(title: "Sign-Up", style: .Default, handler: signupHandler))
presentViewController(actionSheet, animated: true, completion: nil)
}
Any help would be greatly appreciated! Thanks in advance.
I dont know about segues but I normally just present them like this :
let loginVC = LoginViewController()
self.presentViewController(loginVC, animated: true, completion: nil)