Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. ##Using UIAlertController with NSLocalizedString
  2. Because I tend to localize everything in my apps, UIAlertController needed to be localized as well. So, I wrote a struct to simplify the way I use NSLocalizedWithString (which happens to be a long piece of code), and so I could use it in all my classes.
  3.  
  4. Use an extension (remember, an extension is like a category in Obj-c)
  5.  
  6. extension String {
  7.  
  8. var localized: String {
  9. return String.localizedStringWithFormat(NSLocalizedString(self,comment: ""))
  10. }
  11.  
  12. func localizedWithComment(comment:String) -> String {
  13. return String.localizedStringWithFormat(NSLocalizedString(self,comment: comment))
  14. }
  15. }
  16.  
  17.  
  18. Use Constants for better manipulation
  19.  
  20. let errortitle = "alertErrorTitle".localized
  21. let errormessage = "alertErrorMessage".localized
  22. let cancel = "cancelAlert".localized
  23.  
  24. And the AlertController piece of code:
  25.  
  26. func showAlertError(){
  27.  
  28. var alertController = UIAlertController(title: errortitle,
  29. message: errormessage,
  30. preferredStyle: .Alert)
  31.  
  32. let cancelAction = UIAlertAction(title: cancel, style: .Cancel) {
  33. (action) in
  34. //whatever action you want, put it here
  35. }
  36. alertController.addAction(cancelAction)
  37. presentViewController(alertController, animated: true, completion: nil)
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement