UIBarButtonItem init with custom view selector not

2019-06-26 20:53发布

问题:

I'm trying for hours to make a left button work properly and mimic a back button.

My code to create the button:

UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];

backButton.target = self;
backButton.action = @selector(buttonPressed:);

self.navigationItem.leftBarButtonItem = backButton;

Here the method called to create custom button:

- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {

UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];

CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);

UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];

[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];

UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

return barCustomButton;

}

And the action:

-(void)buttonPressed:(id) sender{

    NSLog(@"Entered");
    SearchViewController *ViewController = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:ViewController animated:YES];

}

So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.

If you could help me I'd be very grateful.

Thanks.

回答1:

Do this add selector to custom button as it is view of bar buttom:

[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.



回答2:

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];


回答3:

TRY THIS

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:@"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:@selector(btnBackClicked:) target:self]];


+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
    UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [objButton setFrame:buttonFrame];
    [objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
    [objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
    if(heighlightImageName)
        [objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
    if(heighlightImageName)
        [objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
    return objButton;
}


回答4:

Try this..

[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

in your customBarButton method.. I mean to say give selector to your button instead of bar button item..

Check and reply..



回答5:

Just put this line in your code..

[backButton addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];


回答6:

This worked for me

UIButton * tmpButton = [self generateButtonwithImageName:@"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;


-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
  UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.frame=rect;
  [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
  return button;
}


回答7:

//working for me

if([version floatValue] < 7.0)
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.opaque = YES;

    closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
    [closeButton setImage:[UIImage imageNamed:@"btn_end.png"] forState:UIControlStateNormal];
    [closeButton setImage:[UIImage imageNamed:@"btn_end_on.png"] forState:UIControlStateHighlighted];
    [closeButton addTarget:self action:@selector(actionClose) forControlEvents:UIControlEventTouchUpInside];


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{


    /*add button back to navigation*//*add buttom back to navigation*/
    UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:@"完了" style:UIBarButtonItemStylePlain target:self action:@selector(actionClose)];
    self.navigationItem.rightBarButtonItem = itemBack;
}