Kalamantina
Kalamantina

Reputation: 303

Creating custom buttons for UIBarButton, selector throwing exception

I am creating a custom button with its own image, and assigning it to the rightbarbutton of a nabvigationcontroller I have. The problem is that when the user clicks on the button I get an exceptoin about my controller not recognizing that selctor??

UIImage* image1 = [UIImage imageNamed:@"someImage.png"];
CGRect imgRect = CGRectMake(0, 0, image1.size.width/1.8, image1.size.height/1.8);
UIButton *myButton = [[UIButton alloc] initWithFrame:imgRect];
[myButton setBackgroundImage:image1 forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(nextScreenButtonAction)   forControlEvents:UIControlEventTouchUpInside];


UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setRightBarButtonItem:myButton];
self.navigationController.navigationBarHidden = false;

Upvotes: 0

Views: 75

Answers (2)

Pratyusha Terli
Pratyusha Terli

Reputation: 2343

May be this is the problem nextScreenButtonAction change it to

[myButton addTarget:self action:@selector(nextScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside]

In your method definition you be passing and argument like

-(IBAction) nextScreenButtonAction:(id)sender
{
} 

so in the addTarget you need to specify a colon(:) to indicate parameter

Upvotes: 1

alloc_iNit
alloc_iNit

Reputation: 5183

In response to below code,

[myButton addTarget:self action:@selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];

Make sure that you have create method in the same class like,

- (void) nextScreenButtonAction
{
   // write your logic here...
}

Upvotes: 0

Related Questions