这是我的代码:
@interface UserProfileViewController : UIViewController<UIGestureRecognizerDelegate>{
}
@property (nonatomic, retain) UILabel *myLabel;
@property (nonatomic, retain) UIImageView *imageView;
@end
@implementation UserProfileViewController
@synthesize myLabel;
@synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 700);
scrollView.clipsToBounds = YES;
[scrollView setUserInteractionEnabled:YES];
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
myLabel.text = @"Kim Gysen";
myLabel.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, 25);
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.textColor = [UIColor whiteColor];
[myLabel setBackgroundColor:[UIColor clearColor]];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 140, 180)];
self.imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, 150);
UIImage *image = [UIImage imageNamed: @"ProfilePic.jpeg"];
[imageView setImage:image];
//Long press
self.imageView.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFrom:)];
longPressGestureRecognizer.delegate = self;
[self.imageView addGestureRecognizer:longPressGestureRecognizer];
[scrollView addSubview:myLabel];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
}
- (void) handleLongPressFrom: (UISwipeGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:self.view];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f", location.x, location.y);
if(UIGestureRecognizerStateBegan == recognizer.state)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture Recognizer Demo"
message:@"Long Press Gesture performed"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}
我的 ViewDidLoad 中没有任何错误,但是在实际触发该方法之前,长按出现 BAD_EXC_ACCESS 错误。我知道我在某处泄漏,但在哪里?我正在使用ARC ...