UIAlertView,UIActionSheet和iOS8推出UIAlertControl的基本使用

  

UIAlerView的基本使用:

 

 1 //创建UIAlertView
 2 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:@"提示信息" delegate:代理 cancelButtonTitle:@"取消按钮otherButtonTitles:@"其他按钮", nil];
 3 // 添加到父视图上
 4      [self.view addSubview:alert];
 5 //唤醒控件
 6      [alert show];
 7 
 8 //代理协议.
 9       - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
10 
11           通过 switch 来进行判断,用户点击了那个按钮,然后进行操作
12 
13 }

UIAlerView上的按钮,每一个按钮都有自己的tag值,我们可以通过判断对应的按钮的tag值,执行某一些操作

   取消按钮的tag值是0,其他的按钮以此增加.

 

UIAlerView 是在屏幕的中心弹出

   

 

 

UIActionSheet的基本使用:

  

 1     //创建UIActionSheet
 2      UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"提示"  delegate:代理 cancelButtonTitle:@"取消按钮" destructiveButtonTitle:@"告按钮" otherButtonTitles:@"其他按钮", nil];
 5      //添加到视图上 
 6      [sheet showInView:self.view];
 7     
 8     //UIActionSheet代理方法:
 9     - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
10         switch (buttonIndex) {
11             case 0:
12                 NSLog(@"销毁按钮"); 
13                 break;
14             case 1:
15                 NSLog(@"其他按钮");
16                 break;
17             case 2:
18                 NSLog(@"取消按钮");
19                 break;
20         }
21 }

 

UIActionSheet 是从屏幕的下面弹出:

 

  

 

 

iOS8以后推出UIAlertControl控件代替上面的两个控件:

  UIAlertControl的基本使用:

    alertControl有两种样式:

      一:UIAlertControllerStyleAlert

 

      二:UIAlertControllerStyleSheet

      

      Alertcontrol的定义类型

    

 

      Action的定义类型

  

    

 

 1 //这个方法创建出来的使我们自定的的弹窗
 2   UIAlertController *alert = [UIAlertController alloc]initWithNibName:<#(nullable NSString *)#> bundle:<#(nullable NSBundle *)#>]
 3     
 4 //创建AlertControl
 5   UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示" message:@"你是🐷吗" preferredStyle:UIAlertControllerStyleAlert];
 6 
 7 //创建alertAction
 8   UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
 9             当点击了按钮要执行的操作,这样我们就可以通过创建的alertAction的block代码块给按钮添加点击事件.
10   }];
11 //添加按钮到alert上
12     [alert addAction:action];
13 //让控制器使用这个弹窗
14     [self presentViewController:alert animated:YES completion:^{
15         当调用方法的时候执行的操作
16     }];

 

  

 

posted @ 2016-02-21 15:35  黑桃A~  阅读(841)  评论(0编辑  收藏  举报