Blocks on the iPhone – PSAlertView

17 Mar
2010

It took a while to get my head around blocks. And it didn’t help that they are not officially available on the iPhone. But savvy devs from Plausible Labs back-ported the GCC extensions that were added to support blocks to the iPhone. And it’s AppStore-safe and they are developing it actively, now adapting it for the iPad. (I already blogged about it)

Today I coded the first actually useful component for blocks. PSAlertView – a wrapper for UIAlertView, no more need for custom delegates. It’s so natural if you think about it.

Some code so you get the idea:

PSAlertView *alert = [PSAlertView alertWithTitle:alertTitle message:alertMessage];
[alert setCancelButtonWithTitle:NSLocalizedString(@"Ok", @"") block:^{}];
[alert addButtonWithTitle:NSLocalizedString(@"Retry", @"") block: ^{
[self sendMessage:message];
}];
[alert show];

No more custom delegates, no need to set the protocol in the class header. You can use variables from the function surrounding the block (message) and there’s no performance impact.

Update:  Check out a full example on github. I’d love to read your thoughts on it!

PSAlertView is loosely based on PLActionSheet, thanks to Landon Fuller for the tutorials.

Related posts:

  1. Retain Cycles with Blocks
  2. Objective C Blocks with Leopard 10.5 and iPhone 3.0
  3. How To Mock ASIHTTPRequest With OCMock And Blocks
  4. Detect if an iPhone is jailbroken
  5. iPhone dev tip of the day: clear your delegates!

2 Responses to Blocks on the iPhone – PSAlertView

Avatar

alex

March 18th, 2010 at 4:55 pm

Great extension, looks a lot like the action-pattern, enriched with the possibility of a more complex code within a block.

So that’s the convenient example of “blocks” we were looking for back in the fall of ’09. ;-)

Avatar

studpete

March 19th, 2010 at 3:43 pm

yeah. once you get your head around it, you wonder why it wasn’t there all the time.

Comment Form

top

Switch to our mobile site