• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
hiei03301
博客园    首页    新随笔    联系   管理    订阅  订阅

UIAlertController警告视图和操作表单

 1 //创建一个myAlert1操作表单对象(UIAlertControllerStyleActionSheet为操作表单,UIAlertControllerStyleAlert为警告视图) 
 2   UIAlertController *myAlert1 = [UIAlertController alertControllerWithTitle:@"Are you sure?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
 3     //创建一个操作表中的按钮对象(警告框、操作表默认没有按钮)
 4     //参数handler参数是个块,具体实现点击这个按钮所需处理的内容
 5     UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
 6         
 7         NSString *msg;
 8         if ([self.nameText.text length] > 0) {
 9             msg = [NSString stringWithFormat:@"Your name is %@",self.nameText.text];
10     
11         }else{
12             msg = @"You haven't input";
13         }
14         
15         UIAlertController *myAlert2 = [UIAlertController alertControllerWithTitle:@"Something Was Done" message:msg preferredStyle:UIAlertControllerStyleAlert];
16         
17         UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Phew!" style:UIAlertActionStyleCancel handler:nil];
18     //往myAlert2警告框对象中添加这个cancel按钮
19         [myAlert2 addAction:cancelAction];
20     //在屏幕中绘制警告框对话框
21         [self presentViewController:myAlert2 animated:YES completion:nil];
22     }];
23     UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No way!" style:UIAlertActionStyleCancel handler:nil];
24     
25     [myAlert1 addAction:yesAction];
26     [myAlert1 addAction:noAction];
27     
28     //------这段代码开发iPhone app时可不写,iPad必须要写-------
29     UIPopoverPresentationController *ppc = myAlert1.popoverPresentationController;
30     
31     if (ppc != nil) {
32         ppc.sourceView = sender;
33         ppc.sourceRect = sender.bounds;
34         //设置iPad操作表单的属性,使箭头方向为往下指
35         ppc.permittedArrowDirections = UIPopoverArrowDirectionDown;
36     }
37     //------这段代码开发iPhone app时可不写,iPad必须要写-------
38     
39     [self presentViewController:myAlert1 animated:YES completion:nil];

 

posted @ 2016-08-09 15:13  hiei03301  阅读(188)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3