我添加了一个向上滑动手势到一个图像,但当滑动时,应用程序崩溃与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。
提前谢谢。
发布于 2014-02-07 13:54:22
如果使用addSubview,请执行以下操作:
[self addChildViewController:myViewController];在那之后:
[self.view addSubView: myViewController.view];然后在视图控制器中使用UISwipeGestureRecognizer。
发布于 2013-05-13 23:17:42
你忘了一个冒号:
swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped)];应该是
swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];如果没有冒号,Obj-C运行时将尝试查找没有任何参数的方法。
发布于 2013-11-05 08:51:15
(正如@Undo所说,您忘记了一个冒号。)
但是,如果您的ViewController是在触摸事件发生之前释放的,您仍然会收到EXC_BAD_ACCESS错误。
当将ViewController的视图作为子视图添加到另一个视图控制器时,可能会发生这种情况。例如:
[mainViewController.view addSubview:self.view]其中,self是您的MyViewController。您可以通过将断点添加到
-(void)dealloc您的MyViewController的。并在你的触摸事件之前检查MyViewController是否被释放。
您可以通过向MyViewController (ARC)添加一个强引用来修复此问题,无论它位于何处。
https://stackoverflow.com/questions/16525260
复制相似问题