shaw2thefloor
shaw2thefloor

Reputation: 600

Programmatically add IBAction to UIButton

I am trying to add an action to a UIButton, but keep getting an exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[UIImageView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x595fba0'

Here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
    self.profileButton = (UIButton*)[[UIImageView alloc] initWithImage:myIcon];

    [self.profileButton addTarget:self action:@selector(profileButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithCustomView:profileButton] autorelease];

    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:buttonItem, nil];

    [self setToolbarItems:toolbarItems animated:NO];

    //[toolbarItems release];
    //[profileButton release];
}

Then I have this method in the same View controller:

-(void)profileButtonPressed:(id)sender{

}

And in the header I have

-(IBAction)profileButtonPressed:(id)sender;

What's going on?

Upvotes: 0

Views: 3157

Answers (5)

ipraba
ipraba

Reputation: 16553

Why are you casting a UIImageView into a button.

UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
self.profileButton = [UIButton buttonWithStyle:UIButtonStyleCustom];
[self.profileButton setImage:myIcon forState:UIControlStateNormal];
[self.profileButton addTarget:self action:@selector(profileButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 3

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You can't cast a UIImageView object to UIButton and expect it to behave like a UIButton. Since you intend to create a UIBarButtonItem, use initWithImage:style:target:action: to init it with an image.

UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:myIcon style:UIBarButtonItemStylePlain target:self action:@selector(profileButtonPressed:)] autorelease]; 

I think this is a better approach over creating a UIButton and assigning it as a custom view.

Upvotes: 2

beryllium
beryllium

Reputation: 29767

Create your own button at first. And add action after:

UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
UIButton *buttonPlay = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPlay.frame = CGRectMake(0, 0, 20, 20);
[buttonPlay setBackgroundImage:myIcon forState:UIControlStateNormal];
[buttonPlay addTarget:self action:@selector(buttonPlayClick:) forControlEvents:UIControlEventTouchUpInside];

And your selector should be like this

- (void)buttonPlayClick:(UIButton*)sender{
}

Now you can create custom bar item

UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithCustomView:buttonPlay] autorelease];

Upvotes: 2

taskinoor
taskinoor

Reputation: 46037

You are casting an UIImageView to UIButton which does not respond to addTarget:action:forControlEvents:. Create an actual button and set image for different states by using setBackgroundImage:forState: or setImage:forState: of UIButton.

Upvotes: 4

filipe
filipe

Reputation: 3380

This looks very wrong:

self.profileButton = (UIButton*)[[UIImageView alloc] initWithImage:myIcon];

A UIImageView is not a UIButton. You should alloc and init a proper UIButton, and then you can call

[self.profileButton setImage: myIcon forState:UIControlStateNormal];

Upvotes: 2

Related Questions