Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Using UIAlertController with NSLocalizedString
- 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.
- Use an extension (remember, an extension is like a category in Obj-c)
- extension String {
- var localized: String {
- return String.localizedStringWithFormat(NSLocalizedString(self,comment: ""))
- }
- func localizedWithComment(comment:String) -> String {
- return String.localizedStringWithFormat(NSLocalizedString(self,comment: comment))
- }
- }
- Use Constants for better manipulation
- let errortitle = "alertErrorTitle".localized
- let errormessage = "alertErrorMessage".localized
- let cancel = "cancelAlert".localized
- And the AlertController piece of code:
- func showAlertError(){
- var alertController = UIAlertController(title: errortitle,
- message: errormessage,
- preferredStyle: .Alert)
- let cancelAction = UIAlertAction(title: cancel, style: .Cancel) {
- (action) in
- //whatever action you want, put it here
- }
- alertController.addAction(cancelAction)
- presentViewController(alertController, animated: true, completion: nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement