iOS開發:創建獨立的警告視圖
作者:佚名
我們向用戶提供了選項,并且需要這些選項,可以通過實現協議里的方法。我們通過以下兩個方法判斷選擇:硬編碼的方式對按鈕的索引做比較,或用switch語句;通過-buttonTitleAtIndex方法,比較字符串。
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
責任編輯:閆佳明
來源:
oschina