Sunday, July 27, 2008

UIActionSheet Example > Use more ActionSheets!


UIActionSheet provides a great way to supply the iPhone user with a series of options. You can't find them in Interface Builder and they haven't made it into any of the example apps in the iPhoneDev center. To add to their usefulness you may choose the type and number of buttons shown.

Here is a quick rundown of how to create a UIActionSheet on an instance of UIViewController:




1) In your viewController.h file add the property
UIActionSheet* actionSheet;


2) Instantiate your UIActionSheet in the loadView method

- (void)loadView {
[super loadView];

actionSheet = [[UIActionSheet alloc] initWithTitle:@"This is an ActionSheet" delegate:self cancelButtonTitle:@"Option One" destructiveButtonTitle:@"Option Two" otherButtonTitles:nil];
}


3) Display your action sheet in viewDidLoad
// Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
//Wrong! [self.view addSubview:actionSheet];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
//
}


That's it. Now you can use them everywhere.

Note: If you choose to not use the default cancel/destruct buttons create your ActionSheet as follows:
actionSheet = [[UIActionSheet alloc] initWithTitle:@"What would you like to do today?"
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
otherButtonTitles:@"DVD's", @"MKV's", @"Blu Ray's", @"all",nil];

Labels:

Friday, July 25, 2008

warning: initialization from distinct Objective-c type (iPhone Development)

This warning is raised by Xcode whenever an implicit or INCORRECT type cast is occurring.
(The AS3 equivilant is the error: Error # 1067: Implicit coercion of a value of type * to an unrelated type *.)

"Distinct Objective-C type" is the type id. Apple docs say, "In Objective-C, object identifiers are a distinct data type: id. This type is defined as a pointer to an object..."

Example Causes:
NSMutableArray* myArraySorted = [myArray sortedArrayUsingSelector:@selector(compare:)];

The above will raise "warning: initialization from distinct Objective-c type" because sortedArrayUsingSelector returns an object of type id. We can correct this by simply casting the return value to the type NSMutableArray* as follows.

NSMutableArray* myArraySorted = (
NSMutableArray*)[myArray sortedArrayUsingSelector:@selector(compare:)];

Finally notice that the incorrect cast that follows will also raise this warning:
NSMutableArray* myArraySorted = (NSString*)[myArray sortedArrayUsingSelector:@selector(compare:)];
Hence, whenever you see: "warning: initialization from distinct Objective-c type" you are performing an implicit cast.

Labels:

Tuesday, July 22, 2008

MD5 hash on iPhone with cocoa and Objective-C

In beta 7 OpenSSL has been removed from the iPhone SDK. However, MD5 is still available.

Simply import CommonCrypto as follows:
#import < CommonCrypto/CommonDigest.h >

Then add this C function to your objective-c class between the @implementation and @end statements (if you like).

NSString* md5( NSString *str )
{
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
];
}


I am sure you can just use NSData for this but this is the way an example was posted on apple forums. Please feel free to add an NSData based solution.

Read the post here "http://discussions.apple.com/thread.jspa?threadID=1509152&tstart=96"

Labels: