Here at the SDW World Headquarters, we are always looking for the best way to do things. Sometimes that’s a new process or product to use, and sometimes it’s fully utilizing the power of the language that we have in front of us.

Objective-C Blocks

Well, while working on a new iOS app, I need to have a dynamic callback executed after a standard animation occurred. Fortunately for me, Apple gave us one of the most powerful features in Objective-C since iOS4.0, blocks

I love blocks, they are still were my favorite toys as a child. But these blocks are a lot more confusing than the ones that I played with. These iOS blocks are more like a puzzle with pieces that change shape. Once you figure out how to use them correctly, they become a powerful tool to use.

Blocks Animation/Callback Use Case

Ok, let me explain …

Let's say I have a UITableView, and I have two buttons at the top like so:

When the user taps the button the left, it scrolls them back to the top. In this use case, I want to display a message after the animation happens. When the user taps the button on the right, I want to scroll to the bottom of the table. In that use case, after the scrolling animation is over, I then want to add another row to the table.

To accomplish both of these iOS scenarios, we're going to play with our blocks.

Objective-C iOS Blocks Example Code

First, I create a block @property on my class, and then I can dynamically change the block to the code that I need to execute after the animation is done. Here are the important pieces:

typedef void (^CompletionBlock)(void);
Create a type that is the block that we will create in order to hold it as a property.
...
@property (copy, nonatomic) CompletionBlock scrollViewCompletionBlock;
Create the class property so that we can dynamically set it be any block of code that we want.
...
- (void)viewDidLoad
{
self.scrollViewCompletionBlock = ^(void){ };
}

Make sure that we have instantiated the block to an empty block so that we don’t create an exception.
...
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
_self.scrollViewCompletionBlock();
self.scrollViewCompletionBlock = ^(void){ };
}_

Execute the code block and then set the value to an empty block so that it doesn’t execute again later when not desired.
...
- (void)buttonTapped:(id)sender
{
self.scrollViewCompletionBlock = ^(void)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hola" message:@"Welcome to the top my friend." delegate:nil cancelButtonTitle:@"OK ... cool" otherButtonTitles:nil];
[alertView show];
}
[self.scrollView scrollRectToVisible:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f) animated:YES];
}

Set the block of code to the code that needs to be executed, then call the animation of the UIScrollView.

Code Summary

While at first glance this seems to be confusing and not very useful, but it can help give clarity to the callbacks that you’re executing on code in you apps. And since UIScrollView is the parent of UITableView it can also be applied there and used to execute after the table has scrolled to a specific location with code. Whether it is a “scroll to top” call, scrolling to the last row and then adding another row to the table, or any other crazy idea that a designer tells you to code out.

Not a game changer, but a better understanding of the different uses of blocks can only make you better.

Grab These iOS Block Examples on GitHub

Here’s some code to show you how it’s done. https://github.com/markflowers/Building-with-Blocks