【问题标题】:unrecognized selector sent to instance - UIButton action无法识别的选择器发送到实例 - UIButton 操作
【发布时间】:2011-02-25 15:40:26
【问题描述】:

大家好,我有一个简单的 UIView,里面有一个 UIButton,调用一个方法,我试图尽可能地简化它,但总是遇到同样的错误:

2011-02-25 10:33:23.423 USOpenTimeLine[21288:207] -[CALayer buttonClicked:]:无法识别的选择器发送到实例 0x4d2a020 2011-02-25 10:33:23.425 USOpenTimeLine[21288:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[CALayer buttonClicked:]:无法识别的选择器发送到实例 0x4d2a020” * 首次抛出时调用堆栈:

我的代码如下:

-(UIView *)createView:(NSArray *)_data{

    UIView *_view = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    pageData = [[NSMutableArray alloc] init]; 
    self.imageData = _data;
    [_view setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0 alpha:1.0]];
    [self addSubview:_view];

    UIButton *_button1= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    _button1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
    [_button1 setTitle:@"Play" forState:UIControlStateNormal];
    _button1.backgroundColor = [UIColor clearColor];
    [_button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_view addSubview:_button1];  

    [_button1 release];
    [_view release];

    return _view;
}

- (IBAction)buttonClicked:(id)sender
{
    NSLog(@"Hi!");
}

我这辈子都想不通这里发生了什么…… 任何帮助或见解将不胜感激! 谢谢

【问题讨论】:

  • 好的,我想通了,我太早释放父视图了。

标签: objective-c ipad uiview uibutton


【解决方案1】:

此代码看起来正确。很可能您遇到了内存管理问题,创建此按钮并实现 buttonClick: 方法的对象消失了,内存现在被一些未实现 buttonClicked 的新对象占用。

开启僵尸检查;这应该可以帮助您找出问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2017-01-26
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多