iOS開(kāi)發(fā):創(chuàng)建獨(dú)立的警告視圖
作者:佚名
我們向用戶(hù)提供了選項(xiàng),并且需要這些選項(xiàng),可以通過(guò)實(shí)現(xiàn)協(xié)議里的方法。我們通過(guò)以下兩個(gè)方法判斷選擇:硬編碼的方式對(duì)按鈕的索引做比較,或用switch語(yǔ)句;通過(guò)-buttonTitleAtIndex方法,比較字符串。
UIAlertView 的基本用法就不再說(shuō)了,如果我們向用戶(hù)提供了選項(xiàng),并且需要這些選項(xiàng),可以通過(guò)實(shí)現(xiàn)協(xié)議里的方法。我們通過(guò)以下兩個(gè)方法判斷選擇:
1,硬編碼的方式對(duì)按鈕的索引做比較,或用switch語(yǔ)句。
2,通過(guò)-buttonTitleAtIndex方法,比較字符串。
如果警告視圖較多,那就不只是判斷是那個(gè)按鈕被按下,而是那個(gè)警告視圖的那個(gè)按鈕了。
我們使用Block能讓這個(gè)過(guò)程更加漂亮。
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
責(zé)任編輯:閆佳明
來(lái)源:
oschina