如何为UIBarButtonItem实现徽章?

时间:2010-09-08 17:19:51

标签: iphone ios ipad

我想在控制SplitView中弹出窗口的UIBarButtonItem上打一个徽章

此代码没有做任何事情:

- (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;
}

以下尝试引发错误:

- (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;
}

错误: 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.'

如何为UIBarButtonItem添加徽章?

1 个答案:

答案 0 :(得分:0)

我就是这样做的,而不是重定向目标和动作,我们为它创建了一个方法,见
-(void)pop:(UIButton *)sender

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

    // configure barButton
    UIButton * button = [UIButton buttonWithType : UIButtonTypeCustom];
    UIImage * image = [UIImage imageNamed:@ "barbutton.png"];
    [button setImage :image forState: UIControlStateNormal];
    button.frame = CGRectMake(0, 0, image.size.width, image.size.width);
    [button addTarget: self 
               action: @selector(pop:) 
     forControlEvents: UIControlEventTouchUpInside];

    barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

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

-(void)pop: (UIButton *)sender {
    [self.popoverController presentPopoverFromRect: sender.frame 
                                            inView: self.view 
                          permittedArrowDirections: UIPopoverArrowDirectionUp 
                                          animated: YES];
}