I am trying to create a button on my app that when pressed will ask the user whether or not they want to add a picture by taking it or by choosing an existing photo from their library. I have been searching all over for the past few days and I have pieced together this code from a few tutorials and some other questions asked on this site. I have this in Xcode and have it connected to a bottom bar button. When I run the simulator and press the button the app freezes or crashes. Any ideas why this is happening?
-(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];
}
Here is the error given in the debug section...
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)
You should not use a bar button item as a view, it is not. The fix is very simple.
UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.barButtonItem = sender;