天天看點

iOS9 提示框的使用

在從iOS8到iOS9的更新過程中,彈出提示框的方式有了很大的改變,在Xcode7 ,iOS9.1的SDK中,已經明确提示不再推薦使用UIAlertView,而隻能使用UIAlertController,我們通過代碼來示範一下。

UIImagePickerController * picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    //初始化提示框;

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:  UIAlertControllerStyleActionSheet];

    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //點選按鈕的響應事件;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        }else{

            NSLog(@"模拟器無法打開相機");

        }

        [self presentViewController:picker animated:YES completion:nil];

    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //點選按鈕的響應事件;

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:nil];

    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        //點選按鈕的響應事件;

    }]];

    //彈出提示框;

    [self presentViewController:alert animated:true completion:nil];

iOS9 提示框的使用

//初始化提示框;

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:  

UIAlertControllerStyleAlert];

iOS9 提示框的使用