2

I have the following code

let alertController = UIAlertController(title: "error_title".localized, message: "error".localized, preferredStyle: .ActionSheet)

    let retryAction = UIAlertAction(
        title: "retry".localized,
        style: .Default,
        handler: {
            (action:UIAlertAction!) in
            self.fetch()
        }
    )
    alertController.addAction(retryAction)

    let cancelAction = UIAlertAction(
        title: "cancel".localized,
        style: .Default,
        handler: {
            (action:UIAlertAction!) in
            self.navigationController!.popViewControllerAnimated(true)
        }
    )
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, completion: nil)

The dialog is shown ok but when I click a button it does not call the handler function. Any idea?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yamila
  • 443
  • 1
  • 9
  • 20
  • Are you talking about clicking on the button `retry`? – Alexey Pichukov Nov 07 '15 at 20:51
  • both buttons aren't working – Yamila Nov 07 '15 at 21:23
  • Have you tried adding breakpoints or logging to see if it's at least getting there? Might sound dumb, but it would be a first good step. – Andy Ibanez Nov 07 '15 at 22:32
  • After replacing handlers' bodies to simple NSLogs, this code works for me. Handlers are called and the Action Sheet hides. Can you put breakpoints in the handlers to make sure they are not called? Does the Action Sheet dismiss when you press these buttons? Maybe the problem lies somewhere else. – Kasia K. Nov 07 '15 at 22:32

2 Answers2

0

You can try this code.

let alertController = UIAlertController(title: "AlertCotrol", message: "A standard alert.", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ... Do something 
}
alertController.addAction(cancelAction)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ... Do something 
}
alertController.addAction(OKAction)

self.presentViewController(alertController, animated: true) {
// ... Do something 
}
Josué H.
  • 1,171
  • 3
  • 15
  • 35
0

For the internationalization where you have:

title: "retry".localized,

Try changing it to something like:

title: (NSLocalizedString("retry" , comment: "Retry something.") as String),

You may do the same for all the other strings that you want to internationalize, in the code you presented they are the strings you put ".localized" in front.

Of course you must have in your bundle the Localizable.strings file already set to the languages you want. If you need there is more about it at How to localize my app with Xcode 4?

Good luck.

Community
  • 1
  • 1
Marco
  • 314
  • 4
  • 9