UIAlertView的基本使用

#import "ViewController.h"

//遵守代理

@interface ViewController ()<UIAlertViewDelegate>

 

@end

 

@implementationViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    /**

     初始化UIAlertView

     */

    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"节日祝福" message:@"新年好,恭喜发财" delegate:self cancelButtonTitle:@"其他" otherButtonTitles:@"确定", nil];

 

    //设置标题信息

    alertview.title = @"UIAlertView";

    alertview.message = @"UIAlertViewMessage";

    //当一个视图有多个UIAlertView  可以用 tag 来区分,这个属性继承自 UIview

    alertview.tag = 0;

    //只读属性,alertview是否可见

    NSLog(@"%d",alertview.visible);

    //通过给定标题添加按钮

    [alertview addButtonWithTitle:@"我是一个新加的按钮"];

    //按钮总数

    NSLog(@"buttonNum:%zd",alertview.numberOfButtons);

    //获取指定索引的按钮标题

    NSLog(@"buttonIndex1:%zd",[alertview buttonTitleAtIndex:1]);

    NSLog(@"buttonIndex2:%zd",[alertview buttonTitleAtIndex:2]);

    //获取取消按钮的索引

    NSLog(@"cencelButtonIndex:%zd",[alertview cancelButtonIndex]);

    //获取第一个其他按钮的索引

     NSLog(@"firstOtherButtonIndex:%zd",[alertview firstOtherButtonIndex]);

    //显示alertview

    //注意UIAlertView调用show显示出来的时候,系统会自动强引用它,不会被释放

    [alertview show];

}

 

#pragma mark----------------------------------------UIAlertView代理方法

 

 

//根据被点击按钮的索引会调用此方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSLog(@"clickButtonAtIndex:%zd",buttonIndex);

}

//AlertView已经消失时会调用此方法

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

{

    NSLog(@"didDismissWithButtonIndex");

}

 

//ALertView即将消失时会调用此方法

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

{

    NSLog(@"willDismissWithButtonIndex");

}

 

//AlertView的取消按钮会调用此方法

-(void)alertViewCancel:(UIAlertView *)alertView

{

    NSLog(@"alertViewCancel");

}

 

//AlertView已经显示时会调用此方法

-(void)didPresentAlertView:(UIAlertView *)alertView

{

    NSLog(@"didPresentAlertView");

}

 

//AlertView即将显示时会调用此方法

-(void)willPresentAlertView:(UIAlertView *)alertView

{

    NSLog(@"willPresentAlertView");

}

@end


你可能感兴趣的:(UIAlertView的基本使用)