0

I am looking at how to do an AlertView that when the button "OK" is pressed it takes you to a specific View Controller called DiveNumberViewController.

I have the AlertView code done (see below) but can't figure out how to have the OK button to the DiveNumberViewController. Any Help is appreciated.

I am using Xcode 6.3 and Swift

var Alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "OK")
        Alert.show()
Scubadivingfool
  • 1,227
  • 2
  • 10
  • 23

2 Answers2

2

Try this instead:

//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

    //index of cancel button
    if buttonIndex == 0
    {
        //add code if needed
    }
    //index of OK button
    if buttonIndex == 1
    {
        //add code to go to your controller
        var divenumberViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DiveNumberViewController") as! DiveNumberViewController

        self.navigationController?.pushViewController(divenumberViewController, animated: true)
    }
}

Also, check your storyboard to be sure to have your controller class and your storyboard id setup inside.

Custom class | class : DiveNumberViewController

Identity | Storyboard ID : DiveNumberViewController

example here:

enter image description here

jregnauld
  • 1,288
  • 18
  • 17
  • @scubadivingfool sorry I didn't have the rights to answer to your comment, so there is my answer. Hope, it helps! – jregnauld Apr 23 '15 at 20:46
  • Copied everything, the error goes away but it still is on the first ViewController and not DiveNumberViewController – Scubadivingfool Apr 23 '15 at 21:02
  • @Scubadivingfool Do you use an UINavigationController which has your viewcontroller (controller with the UIAlertView) as a rootController? If not, this is normal it doesn't work, because it's the UINavigationController which allows you to go to DiveNumberViewController. Take a look on these links to understand how it works : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/ http://www.ioscreator.com/tutorials/navigate-with-uinavigationcontroller-in-ios7 – jregnauld Apr 23 '15 at 21:10
  • I am currently using a TabViewController. I have AlertViewViewController as the first tab and DiveNumberViewController as the second. The code from here is in the AlertViewViewController – Scubadivingfool Apr 23 '15 at 21:11
  • @Scubadivingfool Ok! So you have two solutions here: 1. if you want to go to the second tabbar(which has DiveNumberViewController) when the user clicks "OK": use this : "self.tabBarController?.selectedIndex = 1" 2. if you want to manage everything in the first tabbar: add a UINavigationcontroller as the first tab bar and this UINavigationController has to have as root controller : AlertViewViewController. You will be able to use self.navigationController?.pushViewController – jregnauld Apr 23 '15 at 21:21
  • Perfect, that works exactly what I want. Can I impose on asking you another question? – Scubadivingfool Apr 23 '15 at 22:12
  • I need to be able to have the AlertView only show up once. Once the DiveNumberViewController has been seen I don't want the AlertView to how up again and I have no clue how to code this part – Scubadivingfool Apr 23 '15 at 22:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76110/discussion-between-jregnauld-and-scubadivingfool). – jregnauld Apr 23 '15 at 23:03
0

You can do like this :

var alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles:"OK")
alert.show()

Or

var alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "Cancel")
alert.addButtonWithTitle("OK")
alert.show()

Don't forget to add the delegate

MyController : UIViewcontroller, UIAlertViewDelegate

When the user will click on the buttons, the delegate will fire this function below. So add your code here to go to DiveNumberViewController

//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

            //index of cancel button
            if buttonIndex == 0
            {
                //add code if needed
            } 
            //index of OK button
            if buttonIndex == 1
            {
                //add code to go to your controller
            }
        }
}

If you use a lot of UIAlertView in a same controller, you can add a tag to each UIAlertView like that. It will allow to add a specific action depending of the UIAlertView which is clicked

alertView.tag = 999

For more information, take a look on Apple's doc : https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html

jregnauld
  • 1,288
  • 18
  • 17