您的位置:首页 > 产品设计 > UI/UE

iOS 中UIAlertController的用法及奔溃处理方法

2016-08-03 00:00 726 查看
摘要: iOS8后 UIAlertController替代了UIActionSheet和UIAlertView,记录一下两者的在UIAlertController的用法.

1.UIAlertView

UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定移除照片吗" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];

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

2.UIActionSheet

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil                                                                             message: nil                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
//添加Button
[alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//处理点击拍照
[self takePhoto];
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//处理点击从相册选取
[self pushImagePickerController];
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];

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

注意:这里有坑...我用手机真机调试和模拟器都表示一切正常,用ipad调试的时候这里崩溃.奔溃位置显示在RunLoop.一直苦思不得其解,后来尝试换用UIAlertControllerStyleAlert时问题得以解决...但是效果不太满意...

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);

使用UIAlertControllerStyleActionSheet时,在ipad奔溃信息如下:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x14dfa6af0>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

解决方法:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil                                                                             message: nil                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
//添加Button
[alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//处理点击拍照
[self takePhoto];
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//处理点击从相册选取
[self pushImagePickerController];
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];

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

UIPopoverPresentationController *popover = alertController.popoverPresentationController;

if (popover) {

popover.sourceView = _publishView;
popover.sourceRect = _publishView.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}

这样就将这个AlertController置于我的_publishiView的下方了.但是这样在ipad上不显示取消按钮,手机端不受影响.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐