Skip to content

Instantly share code, notes, and snippets.

@Mekajiki
Created June 22, 2012 10:01
Show Gist options
  • Save Mekajiki/2971808 to your computer and use it in GitHub Desktop.
Save Mekajiki/2971808 to your computer and use it in GitHub Desktop.
Showing UIAlertView and calling back by blocks

The block syntax was introduced to iOS 4.0. This class enable you to write the code for showing alert and the call back blocks gathered in one place as bellow,

[MyAlertView alertWithTitle:@"Title" message:"some message"
                      cancelButtonTitle:@"OK"
clickedBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                          switch (buttonIndex) {
                                //do something depending on the button index.
                          }
                      } cancelBlock:^(UIAlertView *alertView) {
                          // do something on cancel button
                      } otherButtonTitles:@"Hoge", nil];
#import <Foundation/Foundation.h>
@interface MyAlertView : NSObject<UIAlertViewDelegate>{
void (^clickedButtonAtIndex_)(UIAlertView* alertView, NSInteger buttonIndex);
void (^cancel_)(UIAlertView* alertView);
}
@property(nonatomic,copy) void (^clickedButtonAtIndex)(UIAlertView* alertView, NSInteger buttonIndex);
@property(nonatomic,copy) void (^cancel)(UIAlertView* alertView);
+(void) alertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle
clickedBlock:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))clickedButtonAtIndex cancelBlock:(void (^)(UIAlertView* alertView))cancel
otherButtonTitles:(NSString*)titles,...;
-(id)initWithClickedBlock:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))clickedButtonAtIndex cancelBlock:(void (^)(UIAlertView* alertView))cancel;
@end
#import "MyAlertView.h"
@implementation MyAlertView
@synthesize clickedButtonAtIndex = clickedButtonAtIndex_;
@synthesize cancel = cancel_;
+(void) alertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle
clickedBlock:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))clickedButtonAtIndex cancelBlock:(void (^)(UIAlertView* alertView))cancel
otherButtonTitles:(NSString*)titles,...;
{
MyAlertView* myAlertView = [[MyAlertView alloc] initWithClickedBlock:clickedButtonAtIndex cancelBlock:cancel];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:myAlertView cancelButtonTitle:cancelButtonTitle otherButtonTitles:titles, nil];
[[alertView autorelease] show];
}
- (void) dealloc;
{
[clickedButtonAtIndex_ release];
[clickedButtonAtIndex_ release];
[super dealloc];
}
-(id)initWithClickedBlock:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))clickedButtonAtIndex cancelBlock:(void (^)(UIAlertView* alertView))cancel;{
self = [super init];
if (self) {
self.clickedButtonAtIndex = clickedButtonAtIndex;
self.cancel = cancel;
}
return self;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;{
clickedButtonAtIndex_(alertView, buttonIndex);
[self release];
}
-(void)alertViewCancel:(UIAlertView *)alertView;
{
cancel_(alertView);
[self release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment