UIAlertController 不显示 crashes/freezes 应用 iOS8

UIAlertController doesn't display and crashes/freezes app in iOS8

我正在尝试在我的应用程序上创建一个按钮,按下该按钮时会询问用户是否要通过拍摄照片或从他们的图库中选择现有照片来添加照片。在过去的几天里,我一直在到处搜索,我已经从一些教程和这个网站上提出的其他一些问题中拼凑了这段代码。我在 Xcode 中有这个,并将它连接到底部栏按钮。当我 运行 模拟器并按下按钮时,应用程序冻结或崩溃。知道为什么会这样吗?

-(IBAction)showAlertAction:(id)sender
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
                                                                              message: nil
                                                                       preferredStyle: UIAlertControllerStyleActionSheet];
    [alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self presentViewController: picker animated: YES completion:NULL];

    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        picker2 = [[UIImagePickerController alloc]init];
        picker2.delegate = self;
        [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController: picker2 animated: YES completion:NULL];

    }]];

    alertController.modalPresentationStyle = UIModalPresentationPopover;

    UIView *senderView = (UIView *) sender;
    UIPopoverPresentationController * popover = alertController.popoverPresentationController;
    popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popover.sourceView = senderView;
    popover.sourceRect = senderView.bounds;

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

这是调试部分给出的错误...

2015-07-08 10:37:13.162 LED Audit[6011:282032] -[UIBarButtonItem bounds]: unrecognized selector sent to instance 0x7b658770
2015-07-08 10:37:13.518 LED Audit[6011:282032] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem bounds]: unrecognized selector sent to instance 0x7b658770'
*** First throw call stack:
(
    0   CoreFoundation                      0x008b9746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x00542a97 objc_exception_throw + 44
    2   CoreFoundation                      0x008c1705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x00808287 ___forwarding___ + 1047
    4   CoreFoundation                      0x008baede __forwarding_prep_1___ + 14
    5   LED Audit                           0x000570ea -[PictureViewController showAlertAction:] + 1130
    6   libobjc.A.dylib                     0x005587cd -[NSObject performSelector:withObject:withObject:] + 84
    7   UIKit                               0x00c79a90 -[UIApplication sendAction:to:from:forEvent:] + 99
    8   UIKit                               0x00fffbba -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    9   libobjc.A.dylib                     0x005587cd -[NSObject performSelector:withObject:withObject:] + 84
    10  UIKit                               0x00c79a90 -[UIApplication sendAction:to:from:forEvent:] + 99
    11  UIKit                               0x00c79a22 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    12  UIKit                               0x00dba18a -[UIControl sendAction:to:forEvent:] + 69
    13  UIKit                               0x00dba5a7 -[UIControl _sendActionsForEvents:withEvent:] + 598
    14  UIKit                               0x00db9811 -[UIControl touchesEnded:withEvent:] + 660
    15  UIKit                               0x00cd1cfa -[UIWindow _sendTouchesForEvent:] + 874
    16  UIKit                               0x00cd27d6 -[UIWindow sendEvent:] + 792
    17  UIKit                               0x00c906d1 -[UIApplication sendEvent:] + 242
    18  UIKit                               0x00ca0b08 _UIApplicationHandleEventFromQueueEvent + 21484
    19  UIKit                               0x00c74337 _UIApplicationHandleEventQueue + 2300
    20  CoreFoundation                      0x007db06f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    21  CoreFoundation                      0x007d0b7d __CFRunLoopDoSources0 + 253
    22  CoreFoundation                      0x007d00d8 __CFRunLoopRun + 952
    23  CoreFoundation                      0x007cfa5b CFRunLoopRunSpecific + 443
    24  CoreFoundation                      0x007cf88b CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x03b1f2c9 GSEventRunModal + 192
    26  GraphicsServices                    0x03b1f106 GSEventRun + 104
    27  UIKit                               0x00c78106 UIApplicationMain + 1526
    28  LED Audit                           0x00058d9a main + 138
    29  libdyld.dylib                       0x02c45ac9 start + 1
    30  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

您不应该将条形按钮项目用作视图,事实并非如此。修复非常简单。

UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.barButtonItem = sender;