JAHelia
JAHelia

Reputation: 7902

adding a custom view to uibarbuttonitem of a popover

After reading apple's example: MultipleDetailViews which demonstrates how to use UIPopOverController with two detail view controllers, I can't figure out how to add a custom view to the barButtonItem which exist in the splitViewController delegate method:

-(void)splitViewController:(UISplitViewController*)svc willHideViewController: (UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {

    self.popoverController = pc;
    barButtonItem.text = @"root view controller";
    self.rootPopoverButtonItem = barButtonItem;

    UIViewController <SubstitutableDetailViewController> *detailViewController = [mySplitViewController.viewControllers objectAtIndex:0];
    [detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}

the popover button works well with its default shape, but when I try something like this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *customImage = [UIImage imageNamed:@"popOver.png"];
[button setBackgroundImage: [UIImage imageNamed:@"popOver.png"] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, customImage.size.width, customImage.size.height);
[button addTarget:self action:@selector(???) forControlEvents:UIControlEventTouchUpInside];
barButtonItem.customView = button;

I do not know what is the selector for this button that will get the popover to show correctly, my popover works well without a selector using the delegate methods of the UISplitViewController, so what kind of selector should I put here ?

Upvotes: 1

Views: 1348

Answers (1)

Kevin Low
Kevin Low

Reputation: 2682

Why are you adding a button to the bar button item instead of an image view? Using an image view (or disabling user interaction on the button) will cause the application to use the bar button item selector and target.

If you still want to use a separate button, then what you can do is:

[button addTarget:barButtonItem.target action:barButtonItem.action forControlEvents:UIControlEventTouchUpInside];

This will copy over the default bar button item target and action.

Upvotes: 4

Related Questions