Search code examples
iphoneiosuibuttonuibarbuttonitemmkpinannotationview

Use BarButtonItem as leftCallOutAccessoryView for MKPinAnnotationView?


The title pretty much explained it. Is it possible? I have tried to assign it like this:

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAnnotation:)];
pinView.leftCalloutAccessoryView = leftButton;

I have also tried casting it as a UIButton and as a UIView. The main reason I want this is because I want that little savebutton already intergrated in UIBarButtonSystemItemSave. How may I achieve this?


Solution

  • No, it's not possible mainly because UIBarButtonItem is not a subclass of UIView.

    Casting it to a UIButton or UIView doesn't work because casting does not convert an object from one type to another. It just let's you inform the compiler that some variable can be treated as some other type (if that variable isn't really of that other type, you'll get an exception or it just won't work).

    UIBarButtonItem is a subclass of UIBarItem (which is a subclass of NSObject) and is designed for use only in a UIToolbar or UINavigationBar.

    I don't recommend trying to put a UIToolbar in a callout just to use UIBarButtonItem.
    Here's a look-alike workaround using UISegmentedControl but a segmented control may not look right in a callout either.

    I suggest using a regular UIButton with a custom image or with button type UIButtonTypeContactAdd (blue circle with plus sign).