在從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];
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TOwQDOyEDN1EDOxEDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
//初始化提示框;
UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:
UIAlertControllerStyleAlert];