Delegates are very useful at times but can be very hard to maintain at other times. With iOS 4, Apple introduced a functional programming aspect to it’s object oriented language, Objective C. (Prior to iOS 4, you could technically use blocks using the plausible blocks toolchain.) In this piece of open source code, we will explore how blocks can really simplify your code by avoiding un-necessary callback delegates.

If you have done iOS development, you would definitely have used, UIAlertViews and UIActionSheets. How many times have you used an UIAlertView twice in the same controller? How many times have to made the mistake of handling the delegate callback in a wrong way? i.e., you write code to handle the callback of AlertView1 but, by oversight it handles the callback of AlertView2. One way to avoid this problem is to tag your alertviews and check the tag in the delegates. However this is a bit clumsy and not an object oriented way of doing it. Let’s see how we can do this efficiently using blocks.

Say hello to blocks

How easy would it be to show an alertview and write the handler then and there without implementing a delegate protocol? Well now you can, with this category addition.

UIAlertView alertViewWithTitle:@"Test" 
                            message:@"Hello World" 
                  otherButtonTitles:[NSArray arrayWithObjects:@"First", @"Second", nil]
                          onDismiss:^(int buttonIndex)
         NSLog(@"%d", buttonIndex);

With the block based code, you are essentially passing the code that has to be executed on the delegate handler right into the method as a block parameter. As you see the above code, you realize that, instead of implementing a callback delegate, you are passing a couple of code blocks that has to be executed when certain buttons are tapped. This way your view controller code remains cleaner without un-necessary callback delegates.

As a matter of fact, the source code also contains an equivalent implementation for UIActionSheet and a bonus method to invoke an action sheet for choosing a photo.

With just a few lines of code, you can display a photo picker that automatically displays necessary buttons depending on the device and allows the user to pick an image.

[UIActionSheet photoPickerWithTitle:@"Choose an avatar" 
                          onPhotoPicked:^(UIImage* image) 
         NSData *data = UIImagePNGRepresentation(image);
         [data writeToFile:@"/Users/<username>/Desktop/test.png" atomically:YES];
        // or upload the image to your server

Source Code

The complete source code is available on Github here. Clone it or fork it or do whatever you want* with the code.

Integrating it into your app

To add the code into your app, just clone the project and drag the folder MKAdditions into your project. Make sure to copy the added files. Start using the methods in the class right away.


As always, you can do whatever you want with this code, whether commercial or non-commercial. But while you redistribute or enhance this code or for the repository, do maintain a link back to this blog post.


Follow me on Twitter