Skip to content

Objective C Quick Methods

by Colin on January 14th, 2010

This post will be updated the more I use Objective C.

Quick UIAlertView

  1. +(void)showPopup:(NSString *)title
  2. message:(NSString *)message
  3. buttonTitle:(NSString *)buttonTitle
  4. popupDelegate:(id)popupDelegate
  5.  
  6. {
  7. /**
  8. * Recreates UIAlertView functionality using 1 line of code:
  9. * example:
  10. * #import "Static.h"
  11. * [Static showPopup:@"Error!"
  12. * message:@"You need an internet connection to view this page."
  13. * buttonTitle:@"OK"
  14. * popupDelegate:nil];
  15. */
  16. UIAlertView *openURLAlert = [[UIAlertView alloc] initWithTitle:title
  17. message:[NSString stringWithFormat:message]
  18. delegate:popupDelegate
  19. cancelButtonTitle:buttonTitle
  20. otherButtonTitles:nil];
  21. [openURLAlert show];
  22. [openURLAlert release];
  23. }

Checking for NULL

  1. NSString *myString = [[NSString alloc] init];
  2. if ([myString length] > 0) {} //check the string's length instead of checking for NULL or nil
  3.  

Auto-Detect Languages (and iPhone current Language)

  1. NSArray *languages = [[NSArray alloc] initWithArray:[NSLocale preferredLanguages]];
  2. for (id language in languages) {
  3. NSLog(@"Language: %@", language);
  4. }
  5. NSLog(@"Default Language: %@", [languages objectAtIndex:0]);

Auto-Detect Country

  1. NSLocale *locale = [NSLocale currentLocale];
  2. NSString *code = [locale objectForKey:NSLocaleCountryCode];
  3. NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:code];
  4. NSLog(@"country name: %@", countryName);

From → iPhone

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS