views:

156

answers:

0

I want to pu a badge on the UIBarButtonItem that controls popover in a SplitView

This code did do nothing:

- (void)splitViewController: (UISplitViewController*)svc 
     willHideViewController:(UIViewController *)aViewController 
          withBarButtonItem:(UIBarButtonItem*)barButtonItem
       forPopoverController: (UIPopoverController*)pc 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [view setBackgroundColor:[UIColor greenColor]];
    UIView *badge = [[UIView alloc] initWithFrame:CGRectMake(36, 0, 8, 8)];
    [badge setBackgroundColor: [UIColor redColor]];
    [view addSubview:badge];
    [badge release];


    UIBarButtonItem *aBBItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    [aBBItem setTarget:barButtonItem.target];
    [aBBItem setAction:barButtonItem.action];
    NSLog(@"%@ %s", aBBItem.target, aBBItem.action);
    [view release];

    barButtonItem.title = @"Bars";
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items insertObject:aBBItem atIndex:0];
    [aBBItem release];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = pc;
}

while the following attempt throws an error:

- (void)splitViewController: (UISplitViewController*)svc 
     willHideViewController:(UIViewController *)aViewController 
          withBarButtonItem:(UIBarButtonItem*)barButtonItem
       forPopoverController: (UIPopoverController*)pc 
{ 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIView *badge = [[UIView alloc] initWithFrame:CGRectMake(36, 0, 8, 8)];
    [badge setBackgroundColor: [UIColor redColor]];
    [button addSubview:badge];
    [badge release];
    [button addTarget:barButtonItem.target action:barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Bars" forState:UIControlStateNormal];

    UIBarButtonItem *aBBItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button release];

    barButtonItem.title = @"Bars";
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items insertObject:aBBItem atIndex:0];
    [toolbar setItems:items animated:YES];
    [aBBItem release];
    [items release];
    self.popoverController = pc;
}

Error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a UIBarButtonItem that is not in a toolbar or navigation bar already.'

How can I add a badge to an UIBarButtonItem?