0

I am trying to switch to another ViewController once I click 'OK' on the AlertController. Currently when I click 'OK' it stays on the current ViewController. How can this be done? thanks.

This code is presented in ViewController1 and I want to change to ViewController4 once I have clicked 'OK' when the AlertController appears.

Code Below:

  @IBAction func submitTapped(sender: AnyObject) {
    print("Validating...")
    validator.validate(self)
    Button1.hidden = false
}

// MARK: ValidationDelegate Methods

func validationSuccessful() {
    print("Validation Success!")
    let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4))
        alert.addAction(defaultAction) - Error Code // Variable used within its own initial value
        self.presentViewController(alert, animated: true, completion: nil)

        } - Error Code //Expected ‘,’ separator

1 Answers1

2

You need to add a handler to the OK action:

let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in
    performSegueWithIdentifier("controller4SequeIdentifier", sender: nil);
})
alert.addAction(defaultAction)
self.presentViewController(alert, animated: true, completion: nil)

In the handler you can switch to the other controller. Note that this will happen after the user presses the OK button. You also need to add a seque to ViewController4 in your storyboard, and give it an identifier to match the one passed to performSegueWithIdentifier.

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • I have added that line of code but I am still running into problems. let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4() alert.addAction(defaultAction) - Error Code // Variable used within its own initial value self.presentViewController(alert, animated: true, completion: nil) } - Error Code //Expected ‘,’ separator – Gurvier Singh Dhillon Dec 28 '15 at 21:59
  • I think you didn't properly placed the closing `}` – Cristik Dec 28 '15 at 22:02
  • I have made changes to the above code in this post to show where I am getting the errors. – Gurvier Singh Dhillon Dec 28 '15 at 22:12
  • @GurvierSinghDhillon I have updated the code sample in my answer, things should be clear now. – Cristik Dec 28 '15 at 22:15
  • Cheers for that, the error have gone. I have run the application and when I click 'OK' it does not change to another view controller. I have followed everything you said. – Gurvier Singh Dhillon Dec 28 '15 at 22:22
  • What code have you used to present `ViewController4`? – Cristik Dec 28 '15 at 22:23
  • Here is - let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4() – Gurvier Singh Dhillon Dec 28 '15 at 22:26
  • The code is incomplete, also post only the relevant code related to `ViewController4` , not all `UIAlertAction` stuff – Cristik Dec 28 '15 at 22:37
  • What do you mean by its incomplete? The way I have presented Viewcontroller4 is already in the question that I posted. I have followed what you said but its still not switching. – Gurvier Singh Dhillon Dec 28 '15 at 22:41
  • I don't see any code that presents ViewController4 in the code you posted, I see only a reference to the class. The only thing that you present is the alert. – Cristik Dec 28 '15 at 23:01
  • The code stated above is whats in my 'Viewcontroller2' I basically have a acton button associated with it. I have another class called 'Viewcontroller4' that i want to switch to once i press 'OK', did you want to see the content in the Viewcontroller4 class? If not then I may not have presented it properly. – Gurvier Singh Dhillon Dec 28 '15 at 23:07
  • How do you want ViewController4 to appear on screen? And how do you create that controller: Storyboard, xib, direct instantiation? – Cristik Dec 28 '15 at 23:11
  • The controller is created by StoryBoard, in terms of how it appears I usually segue between both view controllers, but in this case I have not done that. I want it to be done programatically when I press 'OK'. – Gurvier Singh Dhillon Dec 28 '15 at 23:15
  • Then you can programatically do a performSegueWithIdentifier in the UIAlertAction handler. – Cristik Dec 28 '15 at 23:32
  • Cheers man, I am new to swift and I appreciate your time and support. Would you mind updating the code above on how this can be written? thanks. – Gurvier Singh Dhillon Dec 28 '15 at 23:35
  • This is another topic, kinda different of the one posted on this page. You can find out more details [on this SO question](http://stackoverflow.com/questions/9176215/understanding-performseguewithidentifier), also for the Swift declaration of the function you can take a look at the [Apple documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/shouldPerformSegueWithIdentifier:sender:). Will update the code, though, so you can see it in action, but you also need to read the documentation to understand it – Cristik Dec 29 '15 at 05:34
  • Thank you, I have marked this post as answered now. – Gurvier Singh Dhillon Dec 29 '15 at 20:45
  • Thank you, too :) Hope I clarified all unclear things :) – Cristik Dec 29 '15 at 20:45