0

我添加了向上滑动手势到图像,但是当滑动时,应用程序出现BAD_EXEC错误。

这就是我所拥有的:

.h 文件:

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
{

    UISwipeGestureRecognizer* swipeUpGesture;
    IBOutlet UIImageView*   myImage;  //Connected from Interface Builder
    IBOutlet UIScrollView*  myScrollView;
}

@property (retain, nonatomic) UISwipeGestureRecognizer* swipeUpGesture;
@property (retain, nonatomic) IBOutlet UIImageView* myImage;
@property (retain, nonatomic) IBOutlet UIScrollView*  myScrollView;

.m 文件:

- (void)viewDidLoad
{

   //myImage is inside of myScrollView

   swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped)];
   [swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
   [swipeUpGesture setDelegate:self];
   [myImage addGestureRecognizer: swipeUpGesture];

}


- (void)swiped:(UISwipeGestureRecognizer*)sentGesture
{
    NSLog (@"swiped");
}

所以基本上,在 内myView,我有myScrollView。在里面myScrollView,我有myImage

当我运行上面的代码时,应用程序一直运行直到我向上滑动,然后它实际上识别出滑动,但没有到达NSLog,崩溃并且我得到BAD_EXEC.

提前致谢。

4

4 回答 4

3

如果您使用addSubview,请执行以下操作:

[self addChildViewController:myViewController];

在那之后:

[self.view addSubView: myViewController.view];

然后在视图控制器中使用 UISwipeGestureRecognizer。

于 2014-02-07T05:54:21.907 回答
2

你有一个签名不匹配。

swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped)];

您的选择器是“滑动”的,没有冒号,这意味着目标 c 运行时将尝试找到一个采用“零”参数的方法。

而且由于您的“刷卡”接受了一个参数,因此运行时在尝试调用该方法并因此崩溃时将无法找到匹配项。

--

将您的 @selector(swiped) 更改为 @selector(swiped:) 它应该可以工作。

于 2013-05-13T15:16:34.823 回答
1

You forgot a colon:

swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped)];

should be

swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];

Without a colon, the Obj-C runtime will try to find a method without any arguments.

于 2013-05-13T15:17:41.587 回答
0

(正如@Undo 所说,您忘记了冒号。)

但是,如果您的 ViewController 在触摸事件发生之前被释放,您仍然会收到 EXC_BAD_ACCESS 错误。

当将 ViewController 的视图作为子视图添加到另一个视图控制器时,可能会发生这种情况。例如

 [mainViewController.view addSubview:self.view]

self 是你的 MyViewController。您可以通过在

-(void)dealloc

MyViewController 的方法。并检查 MyViewController 是否在您的触摸事件之前被释放。

您可以通过在实例化它的任何位置添加对 MyViewController (ARC) 的强引用来解决此问题。

于 2013-11-05T00:51:15.270 回答