programmatically push to a new viewController after pressing uiAlert button - ios

I was wondering if it is possible to push to a newViewController after pressing a button in the UiAlertController
my code looks like
#objc func handleAcceptRequest() {
let user = self.user
let username = user?.name
let alert = UIAlertController(title: "Are you Sure? ",message:" Would you like to accept \(username!) to complete your job?", preferredStyle: UIAlertController.Style.alert)
let cancelButton = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
print("you pressed cancel button")
})
let continueButton = UIAlertAction(title: "Continue", style: .default, handler: {(_ action: UIAlertAction) -> Void in
let vc = viewController()
let navController = UINavigationController(rootViewController: vc)
print("you pressed Continue")
})
continueButton.setValue(GREEN_Theme, forKey: "titleTextColor")
cancelButton.setValue(UIColor.red, forKey: "titleTextColor")
alert.addAction(cancelButton)
alert.addAction(continueButton)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
I would only like to present the VC if the the Continue button is pressed but If I call the present
// self.present(navController, animated: true, completion: nil)
inside of the continueButton, I get the error Use of unresolved identifier 'present'
is it possible to push a newVC this way?

This is not right
// self.present(navController, animated: true, completion: nil)
It should be:
self.window?.rootViewController?. present(navController, animated: true, completion: nil)

Related

UIAlertViewcontroller is not being popped before going to viewcontroller

I want after signup the alert controller should pop up and then go the loginFirstViewController but this is not going to happen why?? it only goes to loginfirstviewcontroller instead of poping up alert controller
if error == nil {
FIRAuth.auth()?.currentUser!.sendEmailVerification(completion: { (error) in
})
print("You have successfully signed up")
//Goes to the Setup page which lets the user take a photo for their profile picture and also chose a username
let alertController = UIAlertController(title: "Successful!", message: "Email Verification link sent", preferredStyle: .alert)
let alertActionOkay = UIAlertAction(title: "Okay", style: .default)
let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginFirstViewController")
self.present(vc!, animated: true, completion: nil)
alertController.addAction(alertActionOkay)
self.present(alertController, animated: true, completion: nil)
}
You directly opened new viewcontroller to prevent this you should add completion handler for uialertaction. When the user press ok button you can open other viewcontroller
let alertController = UIAlertController(title: "Successful!", message: "Email Verification link sent", preferredStyle: .alert)
let alertActionOkay = UIAlertAction(title: "Okay", style: .default) { (action) in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginFirstViewController")
self.present(vc!, animated: true, completion: nil)
}
alertController.addAction(alertActionOkay)
self.present(alertController, animated: true, completion: nil)

Navigation does not appear Xcode 8.1 iOS 10 Swift 3

Navigation bar does not appear after login button pressed when it moves to Home view controller and I have setup my storyboard and put all thing well but I think i am stuck at this point in code.
Actually there is no segue but coding instead so How I do navigation working?
Code I used to perform Navigation
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
#IBAction func loginAction(_ sender: Any) {
if self.emailTextField.text == "" || self.passwordTextField.text == "" {
//Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
FIRAuth.auth()?.signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
if error == nil {
//Print into the console if successfully logged in
print("You have successfully logged in")
//Go to the HomeViewController if the login is sucessful
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
} else {
//Tells the user that there is an error and then gets firebase to tell them the error
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
Replace your code:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
With this Code:
let vc : YourViewController = self.storyboard?.instantiateViewController(withIdentifier: "Home")as! YorViewController
let navController = UINavigationController(rootViewController: vc)
self.present(navController, animated: true, completion: nil)
You are just trying to present the view controller with out Navigation. Give it Navigation programmatically then it will show Navigation bar.
Are you really using navigationController? If so, should it be pushViewController rather than present?
navigationController?.pushViewController(vc, animated: true)

Swift - push alert action to show new ViewController

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 !!

Change view when clicking alert button not working

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)

Trying to segue to another view controller using an alert action, in Swift

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)

Resources