iOS 中UIAlertController的用法及奔溃处理方法
2016-08-03 00:00
726 查看
摘要: iOS8后 UIAlertController替代了UIActionSheet和UIAlertView,记录一下两者的在UIAlertController的用法.
1.UIAlertView
2.UIActionSheet
注意:这里有坑...我用手机真机调试和模拟器都表示一切正常,用ipad调试的时候这里崩溃.奔溃位置显示在RunLoop.一直苦思不得其解,后来尝试换用UIAlertControllerStyleAlert时问题得以解决...但是效果不太满意...
使用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.'
解决方法:
这样就将这个AlertController置于我的_publishiView的下方了.但是这样在ipad上不显示取消按钮,手机端不受影响.
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上不显示取消按钮,手机端不受影响.
相关文章推荐
- IOS开发:UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- iOS UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- IOS 应用事件处理方法分析
- JS窗口问题处理:使弹出窗口保持前端显示的几种方法,及window窗体对象open()和showModalDialog()用法
- IOS文件路径处理方法
- direct=true 错误处理方法 (关于 Oracle 的数据导入导出及 Sql Loader (sqlldr) 的用法
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- JS窗口问题处理:使弹出窗口保持前端显示的几种方法,及window窗体对象open()和showModalDialog()用法
- JS窗口问题处理:使弹出窗口保持前端显示的几种方法,及window窗体对象open()和showModalDialog()用法
- 关于百度地图iOS中 paopaoView 警告的处理方法
- NSPredicate的用法--iOS里处理正则表达式
- UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
- iOS中与Java中的方法参数不固定时在方法体内处理这些参数的方式对比
- IOS-iPad Hide UIActionSheet button处理方法
- direct=true 错误处理方法 (关于 Oracle 的数据导入导出及 Sql Loader (sqlldr) 的用法
- 心得11--案例分析request.getparameter()方法的用法及请求乱码处理、分配器方法
- 找不到php_mbstring.dll或php_exif.dll的解决方法与mb_系列函数的用法 libmcrypt.dll时出错的处理
- iOS中正确处理dealloc方法
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- IOS之字符串、数组、字典的处理方法