UIAlertView 的基本用法就不再说了,如果我们向用户提供了选项,并且需要这些选项,可以通过实现协议里的方法。我们通过以下两个方法判断选择:
1,硬编码的方式对按钮的索引做比较,或用switch语句。
2,通过-buttonTitleAtIndex方法,比较字符串。
如果警告视图较多,那就不只是判断是那个按钮被按下,而是那个警告视图的那个按钮了。
我们使用Block能让这个过程更加漂亮。
XYAlertView.h:
- #import <UIKit/UIKit.h>
- typedef void(^XYAlertBlock)(void);
- @interface XYAlertView : UIAlertView<UIAlertViewDelegate>{
- }
- + (void)showWithTitle:(NSString *)title
- message:(NSString *)message
- buttonTitle:(NSString *)buttonTitle;
- + (void)showWithTitle:(NSString *)title
- message:(NSString *)message
- cancelTitle:(NSString *)cancelTitle
- cancelBlock:(XYAlertBlock)cancelBlock
- otherTitle:(NSString *)otherTitle
- otherBlock:(XYAlertBlock)otherBlock;
- @end
XYAlertView.m:
- #import "XYAlertView.h"
- @interface XYAlertView ()
- @property (nonatomic, copy) XYAlertBlock cancelBlock;
- @property (nonatomic, copy) XYAlertBlock otherBlock;
- @property (nonatomic, copy) NSString *cancelButtonTitle;
- @property (nonatomic, copy) NSString *otherButtonTitle;
- - (id)initWithTitle:(NSString *)title
- message:(NSString *)message
- cancelTitle:(NSString *)cancelTitle
- ancelBlock:(XYAlertBlock)cancelBolck
- otherTitle:(NSString *)otherTitle
- otherBlock:(XYAlertBlock)otherBlock;
- @implementation XYAlertView
- @synthesize cancelBlock = _cancelBlock;
- @synthesize otherBlock = _otherBlock;
- @synthesize cancelButtonTitle = _cancelButtonTitle;
- @synthesize otherButtonTitle = _otherButtonTitle;
- + (void)showWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)buttonTitle{
- [self showWithTitle:title message:message cancelTitle:buttonTitle cancelBlock:nil otherTitle:nil otherBlock:nil];
- }
- + (void)showWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(XYAlertBlock)cancelBlock otherTitle:(NSString *)otherTitle otherBlock:(XYAlertBlock)otherBlock{
- [[[self alloc] initWithTitle:title message:message cancelTitle:cancelTitle ancelBlock:cancelBlock otherTitle:otherTitle otherBlock:otherBlock] show];
- }
- - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle ancelBlock:(XYAlertBlock)cancelBolck otherTitle:(NSString *)otherTitle otherBlock:(XYAlertBlock)otherBlock{
- if ((self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:otherTitle, nil])) {
- if (cancelBolck == nil && otherBlock == nil) {
- self.delegate = nil;
- }
- self.cancelButtonTitle = cancelTitle;
- self.otherButtonTitle = otherTitle;
- self.cancelBlock = cancelBolck;
- self.otherBlock = otherBlock;
- }
- return self;
- }
- #pragma mark -
- #pragma mark UIAlertViewDelegate
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
- if ([buttonTitle isEqualToString:self.cancelButtonTitle]) {
- if (self.cancelBlock) {
- self.cancelBlock();
- }
- }else if ([buttonTitle isEqualToString:self.otherButtonTitle]){
- if (self.otherBlock) {
- self.otherBlock();
- }
- }
- }
- @end