0

我有一个应用程序因内存错误而崩溃。我打开了僵尸并在调试器中收到此消息:

Sample page based application (xcode template)[32224:f803] ***
-[DataViewController respondsToSelector:]: message sent to deallocated instance 0x6895050

当我添加一个滚动视图和一个支持滚动和双击的图像视图时,就会发生这种情况。双击并快速单击以在页面之间移动(在模拟器中)最终会导致崩溃。

重现:使用 Xcode 4.2,创建一个新项目,基于页面的应用程序。

修改 RootViewController.m,注释掉像这样设置 gestureRecognisers 的代码(以便可以使用滚动视图双击手势):

// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
//self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

这是一个完整的、修改过的 DataViewController.m,它会导致问题。只需使用 Xcode 模板创建项目,替换 DataViewController.m 中的代码,从互联网上下载合适的图像(我使用http://images.apple.com/home/images/hero.jpg)并放入您的项目,您应该能够重现该问题。

DataViewController.m 的新代码

//
//  DataViewController.m
//  Sample page based application (xcode template)
//
//

#import "DataViewController.h"

@interface DataViewController() {

    UIImageView *_imageView; 
    UIScrollView *_scrollView;
}
@end

@implementation DataViewController 

@synthesize dataLabel = _dataLabel;
@synthesize dataObject = _dataObject;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
    tapgr.numberOfTapsRequired = 2;
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_scrollView];
    [_scrollView addGestureRecognizer:tapgr];
    _scrollView.delegate = self;
    UIImage *image = [UIImage imageNamed:@"hero.jpg"];
    _imageView = [[UIImageView alloc]initWithImage:image];
    [_scrollView addSubview: _imageView];
    _scrollView.contentSize = image.size;
    _scrollView.maximumZoomScale = 3.0;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.dataLabel.text = [self.dataObject description];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
- (void)doubleTap:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (_scrollView.zoomScale > _scrollView.minimumZoomScale)
            [_scrollView setZoomScale:_scrollView.minimumZoomScale animated:YES];
        else
            [_scrollView setZoomScale:_scrollView.maximumZoomScale animated:YES];
    }

}


@end

现在只需尝试单击直到它崩溃。任何想法如何解决这个问题?

保罗

4

0 回答 0