`

IOS之UIAlertView的事件处理(免Delegate)

 
阅读更多

        利用这种方式可以避免继承Delegate,不然有很多UIAlertview的时候,处理起来就麻烦了。有效地达到了代码分层的好处。

1.添加头文件,文件请见附件。具体用法可以参照:https://github.com/jivadevoe/UIAlertView-Blocks

#include "RIButtonItem.h"
#include "UIAlertView+Blocks.h"

 2.实现代码,下面的方法是一个UIButton触发的点击事件的自定义方法

//弹出警告框,并实现警告框按钮的触发事件
- (IBAction)showAlert:(UIButton *)sender forEvent:(UIEvent *)event {
    
    RIButtonItem *cancelItem = [RIButtonItem item];
    cancelItem.label = @"No";
    cancelItem.action = ^
    {
        //为NO时的处理
        NSLog(@"为NO时的处理");
    };
    
    RIButtonItem *confirmItem = [RIButtonItem item];
    confirmItem.label = @"Yes";
    confirmItem.action = ^
    {
        //为YES时的处理
        NSLog(@"为YES时的处理");
    };
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete This Item?"
                                                                                message:@"Are you sure you want to delete this really important thing?"
                                                                       cancelButtonItem:cancelItem
                                                                       otherButtonItems:confirmItem, nil];
    [alert show];

}

 

 

  • 大小: 75.8 KB
分享到:
评论

相关推荐

    ios UIAlertView修攺其布局,自定义UIAlertView

    1.系统的UIAlertView使用方便,但是有时需要三个按钮,这时的UIAlertView会出现问题,三个按钮的默认摆放位置,不尽人意 2.使用UIAlertView的Delegate,来修改其布局

    iOS中UIAlertView3秒后消失的两种实现方法

    一,效果图。... UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil message:@此信息3秒后消失 delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil]; [alert show]; [self p

    iOS中UIAlertView警告框组件的使用教程

    – (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /**/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,...

    ios-弹出视图,支持任何定制,整合UIAlertView和UIActionSheet两种风格的弹框,支持iOS7.zip

    整合了UIAlertView和UIActionSheet这两种系统风格的弹框,创建弹框再也不用兼容iOS8以前和iOS8以后了。代码也不会因为delegate而分离,直接接受Block回调。如果您觉得我的代码帮到了您,我会非常欣慰。同时如果您想...

    ios-一个简单好用的选择器--LTPickerView.zip

    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"LTPickerView" message:[NSString stringWithFormat:@"选择了第%d行的%@",num,str] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", ...

    ios版本的helloworld

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"hello" message:@"zhu" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; } 8.右键单击View中添加的按钮分别添加...

    ios开发小技巧

    iOS开发之UIlabel多行文字自动换行 (自动折行) UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, ...

    IOS 中弹框的实现方法整理

    IOS 中弹框的实现方法整理 #define iOS8Later ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) ios 8以前的弹框 @interface RootViewController ()<UIAlertViewDelegate> @end UIAlertView * ...

    ios开发记录

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; //show 展现 ,显示alertview [alertView ...

    IOS5 Programming Cookbook

    iOS 5 Programming Cookbook by Vandad Nahavandipoor Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix...

    FeatureAPIiOS8:了解 iOS 8 中的新 API,例如 UIAlertController、UIVisualEffectView、PhotosKit..

    但是通过delegate 方法处理按钮事件并不方便。 现在我们可以向 UIAlertAction 添加按钮事件,我们可以通过 UIAlertAction 使用 Block。 那么 UIAlertController 有两种样式:UIAlertControllerStyleActionSheet、...

    支持自定义弹出视图功能

    动画效果和外观非常匹配iOS 7中的UIAlertView;警示框支持多个按钮和数量各异的文本;可选自定义内容视图;可更换标题/信息/内容视图;可为独特的警示提醒自定义主题;可为独特的警示提醒自定义文本框 可自定义按钮 ...

    FSAlertView:自定义 UIView 替换 UIAlertView 可以包含很多 Button,FSAlertView 会放置在 Top Bar 和 Bottom Bar 之间并自动滚动良好

    Init、Delegate 也是如此。 在 iOS5、6、7、8 上运行。 不使用ARC。你需要什么 如何使用- ( void )more{ FSAlertView *moreAlert = [[FSAlertView alloc ] initWithTitle: @" title " message: @" the FSAlertView ...

    自动处理本地通知功能

    源码LKAlarmManager, 方便快捷的把 “您的提醒” 加入到 日历或者本地通知中,会自动处理本地通知超过64个的情况,注册下 LKAlarmManager 回调,在接收到提醒的时候,就可以做你想做的事。 使用方法: 使用例子 :...

    手势开启隐藏功能

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"开启隐藏功能" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertView show]; }];

    CC开放平台服务CCOpenServiceLite.zip

    目前市面上提供的第三方SDK,相当繁琐臃肿,还需要去他们的集成平台上注册各种key之类的,相当麻烦.CC轻量级开放平台服务提供一行代码请求数据服务,底层集成了各平台SDK,用户无需关心具体平台的实现,省事省心....

    Table Alert(iPhone源代码)

    来源:Licence:BSD平台:iOS设备:...这份代码实现的Alert View是基于UIView,而不是UIAlertView,使用的是 Block 技术,即不需要delegate函数。支持ARC。 Code4App编译测试,适用环境:Xcode 4.5, iOS 5.0 以上。

    Block UI(iPhone源代码)

     为UIAlertView、UIActionSheet、UIControl的所有子类(UIButton,UIDataPicker 等等)添加使用 Block 的回调,代替delegate和target 和 selector。 小编注:感谢开发者@张玺cc 分享代码于Code4App。 Code4App编译...

    仿腾讯QQ音乐的ActionSheet效果

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您当前点击的是第%d个按钮",index] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];...

Global site tag (gtag.js) - Google Analytics