首页 > 技术文章 > iOS之UIAlertView的使用

calence 2016-03-23 22:57 原文

UIAlertView:

 

1.普通使用:

    //普通的alert

    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];

    [av show];

 

2.UIAlertView还可以设置其样式:

如果可以输入账号与密码的样式:

    //普通的alert

    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];

    [av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];//设置样式:明文与暗文

    [av show];

 

3.判断用户点击了alert的哪个按钮的协议:

 

//协议:alertView

//判断用户点击了alert的那个按钮

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

{

    if(buttonIndex == 0)

    {

        NSLog(@"you click no");

    }

    else

    {

        NSLog(@"you click yes");

    }

}

 

4.   常用方法总结:

  1. //根据被点击按钮的索引处理点击事件  
  2. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  3. {  
  4.     NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
  5. }  
  6.   
  7. //AlertView已经消失时执行的事件  
  8. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  9. {  
  10.     NSLog(@"didDismissWithButtonIndex");  
  11. }  
  12.   
  13. //ALertView即将消失时的事件  
  14. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
  15. {  
  16.     NSLog(@"willDismissWithButtonIndex");  
  17. }  
  18.   
  19. //AlertView的取消按钮的事件  
  20. -(void)alertViewCancel:(UIAlertView *)alertView  
  21. {  
  22.     NSLog(@"alertViewCancel");  
  23. }  
  24.   
  25. //AlertView已经显示时的事件  
  26. -(void)didPresentAlertView:(UIAlertView *)alertView  
  27. {  
  28.     NSLog(@"didPresentAlertView");  
  29. }  
  30.   
  31. //AlertView即将显示时  
  32. -(void)willPresentAlertView:(UIAlertView *)alertView  
  33. {  
  34.     NSLog(@"willPresentAlertView");  

推荐阅读