tags:

views:

192

answers:

1

I am getting an error message displayed below when my button is pressed. I do not yet have any code inside my "buttonPressed" method although I don't think that has anything to do with it?

Error Message: "terminate called after throwing an instance of 'NSException' Program received signal: "SIGABRT".

UIButton * myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[myButton setImage:[UIImage imageNamed:@"ButtonStandard.png"] forState:UIControlStateNormal]; [myButton setImage:[UIImage imageNamed:@"ButtonSelected.png"] forState:UIControlStateSelected]; [myButton setShowsTouchWhenHighlighted:YES];

myButton.frame = CGRectMake(0.0, 380.0, 320.0, 100.0);
[myButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:myButton];

any help would be great.

+2  A: 

Change the @selector(buttonPressed) to @selector(buttonPressed:) (note the colon at the end) and change the method itself to:

-(void)buttonPressed:(id)sender {
    /* sender will be the UIButton. */
}
John Franklin
Actions can be declared both with and without a parameter.
Frank Schmitt
I was getting this error after further looking 2010-01-06 20:59:47.704 SHS[3844:207] *** -[SHSViewController buttonPressed:]: unrecognized selector sent to instance 0x11aa30so I moved my method to SHSViewController and @selector(buttonPressed:) is working fine now. I wonder why it was expecting the method to come from SHSViewController and not the imported class it was in? Anyone know? Thanks to both of you, you each helped in a different way!
I00I
If the code snippet above is part of the SHSViewController, then the reference to self in the addTarget:action:forControlEvents: method call refers to the SHSViewController object, not the button object. If your code above is *not* in the SHSViewController, you have a memory leak.In Objective-C parlance, the addTarget:action:forControlEvents: message asks the object myButton to send the buttonPressed: message to the object self when the specified control event is triggered. Thus, self must accept the buttonPressed: message. Here, self is the object creating the button, the SHSViewController.
John Franklin