When I press the nextButton, which is UIBarButton. Then associated action is not performed.
UIButton *nextButton1=[UIButton buttonWithType:UIButtonTypeCustom];
[nextButton1 setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpOutside];
[nextButton1 sizeToFit];
UIBarButtonItem *nextButton=[[UIBarButtonItem alloc]initWithCustomView:nextButton1];
self.navigationItem.rightBarButtonItem=nextButton;
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];
you are passing UIControlEventTouchUpOutside
, should be UIControlEventTouchUpInside
you passed the forcontrolevent as UIControlEventTouchUpoutside instead of that use
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];
The obvious mistake is UIControlEventTouchUpOutside
, you should change it to UIControlEventTouchUpInside
.
But instead of making a separate button and then assigning it as a view to the UIBarButtonItem
, you can simply make a UIBarButtonItem
with the desired image and associate the desired action with it.
For example:
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(goToItems)];
self.navigationItem.rightBarButtonItem = nextButton;
[nextButton release];