消息发送到iCarousel内的UIButton上的已分配实例

时间:2014-07-01 08:55:56

标签: ios objective-c uibutton uigesturerecognizer icarousel

我已经将iCarousel(https://github.com/nicklockwood/iCarousel)实现到我的Xcode项目中,现在有一个滚动的视图轮播。在每个视图中我都有一个UIButton,我已经添加了一个UILongPressGestureRecogniser,如下所示:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] init];
[lpgr setMinimumPressDuration:1.5];
[lpgr addTarget:self action:@selector(testAction)];
[self.demoButton addGestureRecognizer:lpgr];

但是,当我点击1.5秒时,控制台中会显示以下错误:

2014-07-01 09:50:08.002 ExampleApp [3117:892602] - [ExampleVC testAction]:发送到解除分配的实例0x15cd7bd20的消息 (LLDB)*

我没有看到任何发布视图的代码部分,所以很困惑。为什么这样,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为可能存在这样的问题:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] init];
[lpgr setMinimumPressDuration:1.5];
[lpgr addTarget:self action:@selector(testAction)];
[self.demoButton addGestureRecognizer:lpgr];

你忘记在这些行之后释放lpgr。

[lpgr release];

如果您错过了这一行,并且由于内存优化,iCarussel会发布演示按钮,您的gestureRecognizer将不会被释放,并会向已解除分配的demoButton发送消息。

并像这样使用LongPress:

[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

并处理:

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture{

    if (gesture.state == UIGestureRecognizerStateEnded) {
        //Do Whatever You want on End of Gesture

    }
    else if (gesture.state == UIGestureRecognizerStateBegan){
        //Do Whatever You want on Began of Gesture
    }
}

并且不要忘记实施<UIGestureRecognizerDelegate>

更新

  

在每个视图中我都有一个UIButton

每个视图中都有一个按钮,但是关于你的代码,我看到你有一个类变量demobutton。因此,当您初始化视图时,请添加如下所示的演示按钮:

    UIView* yourView ...

    UIButton* demoButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [yourView addSubview:demoButton];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] init];
    [lpgr setMinimumPressDuration:1.5];
    [lpgr addTarget:self action:@selector(testAction:)];
    [demoButton addGestureRecognizer:lpgr];

    [demoButton release];
    [lpgr release];