iOS, unrecognized selector sent to instance?

会有一股神秘感。 提交于 2019-12-10 13:17:17

问题


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^^?


回答1:


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!



来源:https://stackoverflow.com/questions/21381641/ios-unrecognized-selector-sent-to-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!