UIBarButtonItem: target-action not working?

后端 未结 11 934
无人及你
无人及你 2020-12-01 08:47

I\'ve got a custom view inside of a UIBarButtonItem, set by calling -initWithCustomView. My bar button item renders fine, but when I tap it, it doe

11条回答
  •  感动是毒
    2020-12-01 09:28

    I had the same problem, but was averse to using a UIButton instead of a custom view for my UIBarButtonItem (per drawnonward's response).

    Alternatively, you could add a UIGestureRecognizer to the custom view before using it to initialize UIBarButtonItem; this appears to work in my project.

    This is how I would modify your original code:

    UIImageView *SOCImageView = [[UIImageView alloc] initWithImage:
                                 [UIImage imageNamed:@"cancel_wide.png"]];
    
    UITapGestureRecognizer *tapGesture = 
           [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                   action:@selector(deselectAll:)];
    [SOCImageView addGestureRecognizer:tapGesture];
    
    SOItem.leftBarButtonItem = 
           [[[UIBarButtonItem alloc] initWithCustomView:SOCImageView] autorelease];
    [tapGesture release];
    [SOCImageView release];
    

提交回复
热议问题