0

Here is the controller I implemented for running on iOS7:

import UIKit


@objc(StartViewController)
class StartViewController: UIViewController, UIAlertViewDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

  }



  override func viewDidAppear(animated: Bool) {

    let confirm:UIAlertView = UIAlertView(title: "sdfsdf", message: "sfsff3333", delegate: self, cancelButtonTitle: "cancel")
    confirm.alertViewStyle = UIAlertViewStyle.Default
    confirm.show()
  }


  // MARK: UIAlertViewDelegate


  func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    println("buttonIndex: "+buttonIndex)
  }

  func alertViewCancel(alertView: UIAlertView) {
    println("cancel")

  }
}

It turns out that none of the functions from UIAlertViewDelegate is ever called even if I set the delegate correctly to self.

When I click on one of the buttons in the alert dialog I got the following output in the console:

0x796d4984

What do I have to do to make the UIAlertViewDelegate methods being called?

Edit: I also did:

confirm.delegate = self

This does not help.

Michael
  • 32,527
  • 49
  • 210
  • 370

4 Answers4

0

When you're making the alertView, do confirm.delegate = self (assuming confirm is your UIAlertView.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
0

The problem was this line: println("buttonIndex: "+buttonIndex)

It must be like this:

println("buttonIndex: \(buttonIndex)")
Michael
  • 32,527
  • 49
  • 210
  • 370
0

Try, doing this. I think, the reason is you haven't assigned the delegate to self(your controller).

override func viewDidAppear(animated: Bool) {

    let confirm:UIAlertView = UIAlertView(title: "sdfsdf", message: "sfsff3333", delegate: self, cancelButtonTitle: "cancel")
    confirm.delegate=self;
    confirm.alertViewStyle = UIAlertViewStyle.Default
    confirm.show()
  }
Natasha
  • 6,651
  • 3
  • 36
  • 58
0

you should try UIAlertController for iOS 8 and UIAlertView for iOS 7. Do something like this:

if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
{
  alert = [[UIAlertView alloc] initWithTitle:@"aa" message:@"asdadad"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
    else
   {
    UIAlertController *alertController = [UIAlertController    alertControllerWithTitle:@"aca" message:@"asdfaf" preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {

                               }];

    [alertController addAction:okAction];

    [self presentViewController:alertController animated:YES completion:nil];
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48