Skip to content

Instantly share code, notes, and snippets.

@hsjunnesson
Created February 25, 2012 22:55
Show Gist options
  • Save hsjunnesson/1911385 to your computer and use it in GitHub Desktop.
Save hsjunnesson/1911385 to your computer and use it in GitHub Desktop.
UIAlertViews are bad for you
//
// HSActionSheet.h
// UIAlertViewBlock
//
// Created by Hans Sjunnesson on 2/25/12.
// Copyright (c) 2012 Hans Sjunnesson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import <UIKit/UIKit.h>
// HSActionSheet handler. Called on dismissing the actionsheet.
typedef void(^HSActionSheetDidDismissBlock)(NSInteger buttonIndex);
@interface HSActionSheet : UIActionSheet <UIActionSheetDelegate>
// Designated initializer. Initializes a UIActionSheet with a title, the block to be called on dismissing this alert, the title of the cancel button, the title of the destructive button, and a nil-terminaded list of other button titles.
- (id)initWithTitle:(NSString *)title dismissBlock:(HSActionSheetDidDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
// The handler which will be called on dismissing this alert view.
@property (weak) HSActionSheetDidDismissBlock dismissBlock;
@end
//
// HSActionSheet.m
// UIAlertViewBlock
//
// Created by Hans Sjunnesson on 2/25/12.
// Copyright (c) 2012 Hans Sjunnesson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "HSActionSheet.h"
@implementation HSActionSheet
@synthesize dismissBlock = dismissBlock_;
// Designated initializer. Initializes a UIActionSheet with a title, the block to be called on dismissing this alert, the title of the cancel button, the title of the destructive button, and a nil-terminaded list of other button titles.
- (id)initWithTitle:(NSString *)title dismissBlock:(HSActionSheetDidDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
if ((self = [super initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil])) {
va_list args;
va_start(args, otherButtonTitles);
for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*)) {
[super addButtonWithTitle:buttonTitle];
}
va_end(args);
self.dismissBlock = dismissBlock;
}
return self;
}
#pragma mark UIActionSheetDelegate implementation.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (self == actionSheet) {
if (self.dismissBlock) {
self.dismissBlock(buttonIndex);
self.dismissBlock = nil; // This helps with retain cycles
}
}
}
@end
//
// HSAlertView.h
// UIAlertViewBlock
//
// Created by Hans Sjunnesson on 2/25/12.
// Copyright (c) 2012 Hans Sjunnesson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import <UIKit/UIKit.h>
// HSAlertView handler. Called on dismissing the alertview.
typedef void(^HSAlertViewDidDismissBlock)(NSInteger buttonIndex);
@interface HSAlertView : UIAlertView <UIAlertViewDelegate>
// Designated initializer. Initializes a UIAlertView with a title, message, the block to be called on dismissing this alert, the title of the cancel button and a nil-terminaded list of other button titles.
- (id)initWithTitle:(NSString *)title message:(NSString *)message dismissBlock:(HSAlertViewDidDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
// The handler which will be called on dismissing this alert view.
@property (weak) HSAlertViewDidDismissBlock dismissBlock;
@end
//
// HSAlertView.m
// UIAlertViewBlock
//
// Created by Hans Sjunnesson on 2/25/12.
// Copyright (c) 2012 Hans Sjunnesson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "HSAlertView.h"
@implementation HSAlertView
@synthesize dismissBlock = dismissBlock_;
// Designated initializer. Initializes a UIAlertView with a title, message, the title of the cancel button and a nil-terminaded list of other button titles.
- (id)initWithTitle:(NSString *)title message:(NSString *)message dismissBlock:(HSAlertViewDidDismissBlock)dismissBlock cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
if ((self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil])) {
va_list args;
va_start(args, otherButtonTitles);
for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*)) {
[super addButtonWithTitle:buttonTitle];
}
va_end(args);
self.dismissBlock = dismissBlock;
}
return self;
}
#pragma mark UIAlertViewDelegate implementation.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (self == alertView) {
if (self.dismissBlock) {
self.dismissBlock(buttonIndex);
self.dismissBlock = nil; // This helps with retain cycles
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment