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

+(void)showPopup:(NSString *)title
         message:(NSString *)message
     buttonTitle:(NSString *)buttonTitle
   popupDelegate:(id)popupDelegate
 
{
	/**
	 * Recreates UIAlertView functionality using 1 line of code:
	 * example:
	 *  #import "Static.h"
	 *  [Static showPopup:@"Error!"
	 *			message:@"You need an internet connection to view this page."
	 *		buttonTitle:@"OK"
	 *	  popupDelegate:nil];
	 */
	UIAlertView *openURLAlert = [[UIAlertView alloc] initWithTitle:title
                        message:[NSString stringWithFormat:message]
                        delegate:popupDelegate
                        cancelButtonTitle:buttonTitle
                        otherButtonTitles:nil];
    [openURLAlert show];
    [openURLAlert release];
}

Checking for NULL

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

Auto-Detect Languages (and iPhone current Language)

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

Auto-Detect Country

NSLocale *locale = [NSLocale currentLocale];
NSString *code = [locale objectForKey:NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:code];
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