iOS,无法识别的选择器发送到实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21381641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:16:10  来源:igfitidea点击:

iOS, unrecognized selector sent to instance?

iosiphonexibibaction

提问by user2408952

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

我有一个带有导入的自定义 actionBar 的主屏幕。我在一个单独的 .xib 文件中创建了这个 actionBar,有一个 .m 和 .h 文件。

I do some graphic setup in my actionBar.m's viewDidLoadlike backgroundColorand some other stuff.

我做我的actionBar.m的某些图形设置viewDidLoadbackgroundColor和一些其他的东西。

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

我也有一个按钮,actionBar我用我通常链接按钮的方式链接了一个IBAction.

I load my actionBar into my mainscreen like this:

我将 actionBar 加载到主屏幕中,如下所示:

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

My actionBar.h:

我的 actionBar.h:

- (IBAction)ActionBarLogoff:(id)sender;

My actionBars.m's method:

我的 actionBars.m 的方法:

-(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

2014-01-27 13:52:21.856 GB_Mobil_DK[2954:60b] -[__NSArrayM ActionBarLogoff:]:无法识别的选择器发送到实例 0x1656d880 2014-01-27 13:52:295Mobil_85DK到应用程序 * TerminationGB_5Mobil_858 [__NSArrayM ActionBarLogoff:]未捕获的异常'NSInvalidArgumentException',原因是: ' - [__ NSArrayM ActionBarLogoff:]:发送到实例0x1656d880无法识别的选择' *第一掷调用堆栈:(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:以未捕获的 NSException 类型异常终止

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

谁能告诉我为什么?最重要的是能帮我解决这个问题^^?

回答by Amar

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

您正在释放actionBar实例并仅保留其view. 如果actionBar实例是按钮操作的响应者,则按钮单击消息将发送到已删除的实例。您应该保留该actionBar实例。一种方法是将其设为 ivar 或retain属性。

Also looks like you are creating a UIViewControllerfor a custom view. Instead you can create just a custom UIViewwith its XIB.

看起来您正在UIViewController为自定义视图创建一个。相反,您可以UIView使用其 XIB创建自定义。

EDIT

编辑

Declare retain property,

申报保留财产,

@property (nonatomic, retain) ActionBarWithLogoff *actionBar;

OR

或者

Simply declare as ivar,

只需声明为 ivar,

@interface YourViewController: UIViewController {
    ActionBarWithLogoff *actionBar;
}

And in deallocmethod,

而在dealloc方法上,

-(void) dealloc {
    //...

    [actionBar release];

    //...
}

Hope that helps!

希望有帮助!