iOS, unrecognized selector sent to instance?

user2408952 picture user2408952 · Jan 27, 2014 · Viewed 41.4k times · Source

I got a mainscreen with a imported custom actionBar. I created this actionBar in a separate .xib file, with a .m and .h file.

I do some graphic setup in my actionBar.m's viewDidLoad like backgroundColor and some other stuff.

I also got a button on this actionBar i linked the way i usually link buttons, with a IBAction.

I load my actionBar into my mainscreen like this:

ActionBarWithLogoff *actionBar = [[ActionBarWithLogoff alloc] initWithNibName:@"ActionBarWithLogoff" bundle:nil];
[topBar addSubview:actionBar.view];
[actionBar release];

My actionBar.h:

- (IBAction)ActionBarLogoff:(id)sender;

My actionBars.m's method:

-(void) ActionBarLogoff:(UIButton *)sender
{
NSLog(@"!!!!!!!!!!ActionBarLogoff");
}

This is were my error steels the picture, when i click the button i get the following error:

2014-01-27 13:52:21.856 GB_Mobil_DK[2954:60b] -[__NSArrayM ActionBarLogoff:]: unrecognized selector sent to instance 0x1656d880 2014-01-27 13:52:21.858 GB_Mobil_DK[2954:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM ActionBarLogoff:]: unrecognized selector sent to instance 0x1656d880' * First throw call stack: (0x2f94be83 0x39ca86c7 0x2f94f7b7 0x2f94e0af 0x2f89cdc8 0x32104da3 0x32104d3f 0x32104d13 0x320f0743 0x3210475b 0x32104425 0x320ff451 0x320d4d79 0x320d3569 0x2f916f1f 0x2f9163e7 0x2f914bd7 0x2f87f471 0x2f87f253 0x345b92eb 0x32134845 0x97985 0x3a1a1ab7) libc++abi.dylib: terminating with uncaught exception of type NSException

Anyone able to tell me why? and most importantly able to help me solve this problem^^?

Answer

Amar picture Amar · Jan 27, 2014

You are releasing the actionBar instance and just retaining its view. If actionBar instance is responder to button action, then button click message is getting sent to deleted instance. You should retain the actionBar instance. One way to do this is making it an ivar or a retain property.

Also looks like you are creating a UIViewController for a custom view. Instead you can create just a custom UIView with its XIB.

EDIT

Declare retain property,

@property (nonatomic, retain) ActionBarWithLogoff *actionBar;

OR

Simply declare as ivar,

@interface YourViewController: UIViewController {
    ActionBarWithLogoff *actionBar;
}

And in dealloc method,

-(void) dealloc {
    //...

    [actionBar release];

    //...
}

Hope that helps!