【UI进阶】UIAlertController替代UIAlertView和UIActionSheet

1、虽然UIAlertView弹窗方式在IOS9.0中已经不怎么使用,但是依然在这边列举一下。其实实现方式特别简单

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"测试弹窗" message:@"UIAlertView此弹窗方式已经淘汰" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        // 显示弹窗
        [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (0 == buttonIndex) {
                NSLog(@"点击事件测试");
            }
}

2、UIActionSheet底部弹出测试,此创建方式也已经不再推荐使用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
      UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:@"UIActionSheet底部弹出测试" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil] ;
      [sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
      if (0 == buttonIndex) {
            NSLog(@"UIActionSheet此种弹窗方式也已经取消");
          }
}

3、UIAlertController是苹果官方推荐的创建弹窗的方式,先创建类似UIAlertView的弹窗

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     
    //  typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    //    UIAlertControllerStyleActionSheet = 0,// 底部弹窗
    //    UIAlertControllerStyleAlert// 中部弹窗
    //  }
    
    //  typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    //    UIAlertActionStyleDefault = 0, // 使用默认方式创建
    //    UIAlertActionStyleCancel,// 取消操作,不对数据做任何改变的时候使用
    //    UIAlertActionStyleDestructive// 当删除或者修改数据的时候使用这个style
    //  }
      // 中部弹窗测试
      UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"中部弹窗测试" message:@"你想要的都在这里" preferredStyle:UIAlertControllerStyleAlert];
      UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
      UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
      [alertView addAction:destructive];
      [alertView addAction:cancel];
      [self presentViewController:alertView animated:YES completion:nil];
     
}

4、然后再创建类似UIActionSheet的弹窗

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
      // 1.底部弹窗测试(即以前的ActionSheet)
      UIAlertController *logout = [UIAlertController alertControllerWithTitle:@"你确定注销?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
      UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];
          }];
      UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            return;
          }];
      [logout addAction:destructive];
      [logout addAction:cancel];
      [self presentViewController:logout animated:YES completion:nil];
}

效果展示:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值