自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開(kāi)發(fā):創(chuàng)建獨(dú)立的警告視圖

移動(dòng)開(kāi)發(fā) iOS
我們向用戶(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:

  1. #import <UIKit/UIKit.h>   
  2.     typedef void(^XYAlertBlock)(void); 
  3.     @interface XYAlertView : UIAlertView<UIAlertViewDelegate>{ 
  4.     } 
  5.     + (void)showWithTitle:(NSString *)title 
  6.                   message:(NSString *)message 
  7.               buttonTitle:(NSString *)buttonTitle; 
  8.     + (void)showWithTitle:(NSString *)title 
  9.                   message:(NSString *)message 
  10.               cancelTitle:(NSString *)cancelTitle 
  11.               cancelBlock:(XYAlertBlock)cancelBlock 
  12.                otherTitle:(NSString *)otherTitle 
  13.                otherBlock:(XYAlertBlock)otherBlock; 
  14.     @end 

XYAlertView.m:

  1. #import "XYAlertView.h"   
  2.     @interface XYAlertView () 
  3.     @property (nonatomic, copy) XYAlertBlock cancelBlock; 
  4.     @property (nonatomic, copy) XYAlertBlock otherBlock; 
  5.     @property (nonatomic, copy) NSString *cancelButtonTitle; 
  6.     @property (nonatomic, copy) NSString *otherButtonTitle; 
  7.     - (id)initWithTitle:(NSString *)title 
  8.                 message:(NSString *)message 
  9.             cancelTitle:(NSString *)cancelTitle 
  10.              ancelBlock:(XYAlertBlock)cancelBolck 
  11.              otherTitle:(NSString *)otherTitle 
  12.              otherBlock:(XYAlertBlock)otherBlock; 
  13.     @implementation XYAlertView 
  14.     @synthesize cancelBlock = _cancelBlock; 
  15.     @synthesize otherBlock = _otherBlock; 
  16.     @synthesize cancelButtonTitle = _cancelButtonTitle; 
  17.     @synthesize otherButtonTitle = _otherButtonTitle; 
  18.     + (void)showWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSString *)buttonTitle{ 
  19.         [self showWithTitle:title message:message cancelTitle:buttonTitle cancelBlock:nil otherTitle:nil otherBlock:nil]; 
  20.     } 
  21.     + (void)showWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(XYAlertBlock)cancelBlock otherTitle:(NSString *)otherTitle otherBlock:(XYAlertBlock)otherBlock{ 
  22.         [[[self alloc] initWithTitle:title message:message cancelTitle:cancelTitle ancelBlock:cancelBlock otherTitle:otherTitle otherBlock:otherBlock] show]; 
  23.     } 
  24.     - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle ancelBlock:(XYAlertBlock)cancelBolck otherTitle:(NSString *)otherTitle otherBlock:(XYAlertBlock)otherBlock{ 
  25.         if ((self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:otherTitle, nil])) { 
  26.             if (cancelBolck == nil && otherBlock == nil) { 
  27.                 self.delegate = nil; 
  28.             } 
  29.             self.cancelButtonTitle = cancelTitle; 
  30.             self.otherButtonTitle = otherTitle; 
  31.             self.cancelBlock = cancelBolck; 
  32.             self.otherBlock = otherBlock; 
  33.         } 
  34.         return self; 
  35.     } 
  36.     #pragma mark - 
  37.     #pragma mark UIAlertViewDelegate 
  38.     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
  39.         NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex]; 
  40.         if ([buttonTitle isEqualToString:self.cancelButtonTitle]) { 
  41.             if (self.cancelBlock) { 
  42.                 self.cancelBlock(); 
  43.             } 
  44.         }else if ([buttonTitle isEqualToString:self.otherButtonTitle]){ 
  45.             if (self.otherBlock) { 
  46.                 self.otherBlock(); 
  47.             } 
  48.         } 
  49.     } 
  50.     @end 

 

源代碼下載:http://down.51cto.com/data/835995

責(zé)任編輯:閆佳明 來(lái)源: oschina
相關(guān)推薦

2013-06-14 13:50:28

iOS開(kāi)發(fā)移動(dòng)開(kāi)發(fā)警告視圖

2013-03-29 11:06:24

iOS開(kāi)發(fā)滾動(dòng)視圖UIScrol

2011-08-09 15:17:07

iOS開(kāi)發(fā)

2014-09-02 10:55:25

iOS開(kāi)發(fā)視圖切換

2010-11-16 10:42:45

Oracle創(chuàng)建視圖

2012-05-10 09:08:07

iOS獨(dú)立開(kāi)發(fā)者

2010-11-11 17:20:51

SQL Server創(chuàng)

2011-04-19 10:38:53

Xcode 4MacRubyiOS

2013-07-21 18:09:21

iOS開(kāi)發(fā)ASIHttpRequ創(chuàng)建和執(zhí)行reques

2014-03-14 13:36:19

獨(dú)立游戲經(jīng)驗(yàn)

2009-04-07 10:45:43

Oracle視圖創(chuàng)建

2015-08-05 10:43:40

開(kāi)發(fā)者開(kāi)發(fā)工具

2015-08-05 14:25:26

開(kāi)發(fā)者開(kāi)發(fā)工具

2018-07-25 14:01:47

iOS開(kāi)發(fā)蘋(píng)果

2015-01-20 17:15:55

iOS源碼滾動(dòng)視圖

2012-05-13 12:43:50

iOS

2012-05-02 23:04:38

iOS

2011-07-08 14:51:34

iPhone 視圖

2015-12-30 14:16:05

iOS動(dòng)畫(huà)視圖渲染

2015-12-23 09:16:33

ios動(dòng)畫(huà)渲染機(jī)制
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)