实现iOS8之前的UIAlertController

iOS9之前一直用的是UIAlertView,iOS8.3之前一直用的是UIActionSheet,在这两者之后一个UIAlertController全部代替,在是低版本上UIActionSheet和UIAlertView还是必须的,但是里面的代理方法使用起来肯定是没有block方便的,在这里实现了block的UIAlertView和UIActionSheet.而且代码没有警告

UIAlertView / UIAlertController

if (iOS7) {
        [[[UIAlertView alloc] initWithTitle:nil message:@"确定删除吗" leftButtonItem:[SFButtonItem itemButtonTitle:@"取消" action:^{

            NSLog(@"点击了取消");
        }] rightButtonItems:[SFButtonItem itemButtonTitle:@"确定" action:^{

            NSLog(@"点击了确定");
        }], nil] show];

    } else {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"确定删除吗" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击了取消");
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击了确定");
        }];
        [alertController addAction:deleteAction];
        [alertController addAction:cancelAction];
    }

UIActionSheet / UIAlertController

if  (iOS7){
        [[[UIActionSheet alloc] initWithTitle:@"提示" cancelButtonItem:[SFButtonItem itemButtonTitle:@"取消" action:^{

        }] destructiveButtonItem:[SFButtonItem itemButtonTitle:@"条目1" action:^{

        }] otherButtonItems:[SFButtonItem itemButtonTitle:@"条目2" action:^{

        }], [SFButtonItem itemButtonTitle:@"条目3" action:^{

        }],nil] showInView:self.view];

    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"条目1" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"条目2" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"条目3" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        }];

        [alertController addAction:alert1];
        [alertController addAction:alert2];
        [alertController addAction:alert3];
        [self presentViewController:alertController animated:YES completion:nil];
    }

Demo下载地址:https://github.com/1170197998/iOS7_iOS9_AlertAndSheet

你可能感兴趣的:(实现iOS8之前的UIAlertController)