0

I am trying to create an alert that when brought up will ask the user if they want to select a photo from their library or take a photo. I am working off of a template from the post UIPopover How do I make a popover with buttons like this?. The template is...

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
                                                                          message: nil
                                                                   preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // Handle Choose Existing Photo here
}]];

alertController.modalPresentationStyle = UIModalPresentationPopover;

UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;

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

however at popover.sourceRect = sender.bounds; xcode gives me an error that states Property 'bounds' not found on object of type '__strong id'. What is this error saying and how is this fixed?

Community
  • 1
  • 1
JJ Stamp
  • 121
  • 2
  • 13

2 Answers2

2

You are likely receiving sender as a parameter to this function with type id, which stands for a generic object. The compiler has no idea that it has a property named bounds, since it could really be any object. To fix this, you need to tell it that sender is actually a UIView * by casting it.

UIView* senderView = (UIView *)sender; 

Then you can perform the next assignment:

popover.sourceView = senderView;
popover.sourceRect = senderView.bounds;
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
  • Is there a reason why on the linked post that the person who posted this template would not include this? – JJ Stamp Jul 06 '15 at 19:51
  • The type may have already been specified in the function argument, see http://stackoverflow.com/questions/19641751/property-tag-not-found-on-object-of-type-strong-id – Erik Godard Jul 06 '15 at 20:01
0

The problem is WHEN you're configuring your popover.

The UIKit framework doesn't create that popoverPresentationController property until the alert controller has begun presentation. It doesn't actually display it until the next display update cycle, so don't worry that it will flash un-configured first.

Configure after presenting the alert controller and you should be fine.

Chris Slowik
  • 2,859
  • 1
  • 14
  • 27