rajomato
rajomato

Reputation: 1177

UIButton calling selector crashes the app

In my app I've got a button which is an instance variable declared in the .h file already. Now after allocating it an assigning at target to it and actually pressing the button, the app crashes giving me following output in the debugger:

-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330
2012-05-04 17:51:02.646 financeAppV2[2595:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330'
*** First throw call stack:
(0x13d1022 0x1562cd6 0x13d2cbd 0x1337ed0 0x1337cb2 0x13d2e99 0x1e14e 0x1e0e6 0xc4ade 0xc4fa7 0xc3d8a 0x2dfa1a 0x13a599e 0x133c640 0x13084c6 0x1307d84 0x1307c9b 0x12ba7d8 0x12ba88a 0x1b626 0x29dd 0x2945)
terminate called throwing an exceptionCurrent language:  auto; currently objective-c

My code looks like following:

saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= frame;
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
[saveButton addSubview:label];

[saveButton addTarget:self action:@selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];

[self.view addSubview:saveButton];


-(void)handleActionSheet:(id)sender{
NSLog(@"working");

}

Any ideas why the app could be crashing?

Upvotes: 1

Views: 1069

Answers (3)

Stephen Darlington
Stephen Darlington

Reputation: 52575

Without seeing the rest of your code it's difficult to be specific, but self is not correctly being retained.

You set up your button correctly (as far as I can see) and point the target to self. Unfortunately, by the time you press the button self has gone out of scope and the same memory is used by an NSString. iOS gets surprised when you send a string the handleActionSheet: message.

Upvotes: 5

Diego Freniche
Diego Freniche

Reputation: 5414

Just tested your code and it works. Maybe the frame it's not properly set

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *saveButton = [[UIButton alloc] init];
    saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbutton"] forState:UIControlStateNormal];
    [saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbuttonpressed"] forState:UIControlStateHighlighted];
    saveButton.alpha=.8;
    saveButton.frame= CGRectMake(5, 5, 200, 40);
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
    label.text = @"Hello";
    [saveButton addSubview:label];

    [saveButton addTarget:self action:@selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:saveButton];

}

-(void)handleActionSheet:(id)sender{
    NSLog(@"working");
}

Upvotes: 0

mbm29414
mbm29414

Reputation: 11598

You're sending a message named initAllIVars: to an __NSCFString object (likely an NSString).

Do a search for that message and try to figure out why the object receiving the message is an NSString.

Upvotes: 1

Related Questions