长按:BAD_EXC_ACCESS

时间:2012-10-02 20:53:45

标签: objective-c xcode uigesturerecognizer

这是我的代码:

@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 ......

1 个答案:

答案 0 :(得分:0)

您应该在主视图控制器(类型为strong)中创建一个指向Profile View Controller的属性,然后使用:

self.profileView = [[UserProfileViewController alloc] init]; 
[self.view addSubview: self.profileView.view];