Created
September 4, 2012 02:20
-
-
Save fmtonakai/3615808 to your computer and use it in GitHub Desktop.
Custom AlertView with Interface Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomAlertView.h | |
// CustomAlert | |
// | |
// Created by masaki.fuke on 2012/08/23. | |
// Copyright (c) 2012年 masaki.fuke. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface CustomAlertView : UIAlertView <UIAlertViewDelegate> | |
typedef void(^CustomAlertViewDismissBlock)(CustomAlertView *alertView, id sender); | |
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil; | |
-(void)setDismissBlock:(CustomAlertViewDismissBlock)dismiss; | |
@property (strong, nonatomic) IBOutlet UIView *customView; | |
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *alertButtons; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomAlertView.m | |
// CustomAlert | |
// | |
// Created by masaki.fuke on 2012/08/23. | |
// Copyright (c) 2012年 masaki.fuke. All rights reserved. | |
// | |
#import "CustomAlertView.h" | |
@implementation CustomAlertView | |
{ | |
CustomAlertViewDismissBlock _dismiss; | |
id _pressedSender; | |
} | |
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super init]; | |
if (self) { | |
NSString *nibName = (nibNameOrNil) ? nibNameOrNil : NSStringFromClass([self class]); | |
UINib *nib = [UINib nibWithNibName:nibName bundle:nibBundleOrNil]; | |
[nib instantiateWithOwner:self options:nil]; | |
self.delegate = self; | |
} | |
return self; | |
} | |
-(void)setDismissBlock:(CustomAlertViewDismissBlock)dismiss | |
{ | |
_dismiss = [dismiss copy]; | |
} | |
-(void)show | |
{ | |
[super show]; | |
// アラートビューの要素をすべて隠す | |
for (UIView *subview in self.subviews) { | |
subview.hidden = YES; | |
} | |
// 中心を一時保存 | |
CGPoint c = self.center; | |
// ビューの大きさをカスタムビューの大きさに合わせる | |
self.bounds = self.customView.bounds; | |
// 中心を戻す | |
self.center = c; | |
[self addSubview:_customView]; | |
_customView.hidden = NO; | |
//ボタンにActionを仕込む | |
for (UIButton *btn in self.alertButtons) { | |
[btn addTarget:self action:@selector(pressedAction:) forControlEvents:UIControlEventTouchUpInside]; | |
} | |
} | |
-(void)pressedAction:(id)sender | |
{ | |
_pressedSender = sender; | |
[self dismissWithClickedButtonIndex:0 animated:YES]; | |
} | |
#pragma mark - UIAlertViewDelegate | |
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
if (_dismiss) { | |
_dismiss(self, _pressedSender); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment