iOS开发:创建独立的警告视图

移动开发 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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

 

源代码下载:http://down.51cto.com/data/835995

责任编辑:闫佳明 来源: oschina
相关推荐

2013-06-14 13:50:28

iOS开发移动开发警告视图

2013-03-29 11:06:24

iOS开发滚动视图UIScrol

2011-08-09 15:17:07

iOS开发

2014-09-02 10:55:25

iOS开发视图切换

2010-11-16 10:42:45

Oracle创建视图

2012-05-10 09:08:07

iOS独立开发者

2010-11-11 17:20:51

SQL Server创

2011-04-19 10:38:53

Xcode 4MacRubyiOS

2013-07-21 18:09:21

iOS开发ASIHttpRequ创建和执行reques

2014-03-14 13:36:19

独立游戏经验

2015-08-05 14:25:26

开发者开发工具

2015-08-05 10:43:40

开发者开发工具

2009-04-07 10:45:43

Oracle视图创建

2018-07-25 14:01:47

iOS开发苹果

2015-01-20 17:15:55

iOS源码滚动视图

2012-05-13 12:43:50

iOS

2012-05-02 23:04:38

iOS

2011-07-08 14:51:34

iPhone 视图

2022-07-26 08:02:33

Android微信程序

2010-11-19 10:48:44

oracle视图
点赞
收藏

51CTO技术栈公众号