UIAlertView基本用法详解和delegate方法调用

1,UIAlertView的基本用法


</pre><pre name="code" class="objc">//调用一个类方法 初始化一个 UIAlertView 对象   当一个按钮上有多个按钮的时候,按钮的索引顺序按照定义按钮时候的顺序排列,第一个是取消,第二个是其它按钮中的第一个,,,,依次排序。
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert Title"
                                                    message:@"alert Content"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancle"
                                          otherButtonTitles:@"Other btns", nil];
    // tag属性继承自 UIView , 通过tag唯一标识一个UIAlertView对象,一个页面中有多个UIAlertView对象的时候 可以通过tag判断是哪个对象触发了事件。
    alert.tag = 100;
    // 标题
    alert.title = @"Title";
    //提示内容
    alert.message = @"Content";
    
    //设置透明度
    [alert setAlpha:0.4];
    //alert的几种样式
    //只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型
    [alert setAlertViewStyle:UIAlertViewStyleDefault]; //默认的样式
//    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; //登陆和密码输入文本框
//    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput]; //普通的文本输入框
//    [alert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; //密码输入框
    
    
    //只读属性 查看alert是否可见
    NSLog(@"%i",alert.visible);
    //动态的增加按钮 返回值是增加的这个按钮的buttonIndex
    [alert addButtonWithTitle:@"addButton"];
    //numberOfButtons属性,alert上按钮的总数
    NSLog(@"number of btn : %d", alert.numberOfButtons);
    //buttonTitleAtIndex: 获取指定索引的按钮标题
    NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);
    NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);
    //cancelButtonIndex 属性,获取取消按钮的索引
    NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);
    //获取第一个非 取消 按钮的索引
    NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);
    //将alert显示出来
    [alert show];


2,UIAlertView代理方法使用


// 点击一个按钮的时候会触发这个事件,在这个事件中可以获取alert和buttonIndew,通过这两个属性对点击事件做出不同响应
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex");
}

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button //////////////////////////
- (void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"- (void)alertViewCancel:(UIAlertView *)alertView");
}

//在 UIAlertView 出现之前,在出现动画之前调用这个方法
- (void)willPresentAlertView:(UIAlertView *)alertView  // before animation and showing view
{
    NSLog(@"- (void)willPresentAlertView:(UIAlertView *)alertView");
}
//在 UIAlertView 出现之后, 动画结束之后,UIAlertView 显示出来之后
- (void)didPresentAlertView:(UIAlertView *)alertView  // after animation
{
    NSLog(@"- (void)didPresentAlertView:(UIAlertView *)alertVieww");
}
//在 UIAlertView 隐藏之前,隐藏动画开始之前执行这个方法
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex // before animation and hiding view
{
    NSLog(@"- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex");
}
//在 UIAlertView 隐藏之后,隐藏动画结束之后执行这个方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  // after animation
{
    NSLog(@"- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex");
}

//返回 NO 时候,第一个非cancle按钮将会被禁用
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView    // Called after edits in any of the default fields added by the style
{
        NSLog(@"- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView");
    return NO;
}
/*
 UIAlertView 几个代理函数的调用顺序:
    1)打开一个alert并点击其上按钮:
        alertViewShouldEnableFirstOtherButton ----> willPresentAlertView ----> didPresentAlertView ----> clickedButtonAtIndex ----> willDismissWithButtonIndex ----> didDismissWithButtonIndex

 */

运行结果:

UI页面输出:



控制台输出:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值