UIWebView版本导致崩溃

时间:2012-03-19 18:25:31

标签: objective-c ios cocoa-touch uiwebview

我有一个UIViewController子类,它有一个UIWebView。它添加了Web视图作为视图的子视图,并在dealloc中发布它。这是相关的代码:

#import "MediaVC.h"

@implementation MediaVC
@synthesize file, repository, server, delegate;

- (void)viewDidLoad {
    [super viewDidLoad];


    webView = [[UIWebView alloc] initWithFrame:[[self view] bounds]];
    [webView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [webView setScalesPageToFit:YES];
    [webView setMultipleTouchEnabled:YES];

    [[self view] addSubview:webView];

    [self displayFile];


    [flex release];
    [buttonDone release];
    [buttonAction release];
}


- (void)displayFile {

    if (!mimeString) {
        [webView loadData:nil 
                 MIMEType:nil 
         textEncodingName:nil 
                  baseURL:nil];


        [SVProgressHUD dismissWithError:@"Can't display this file." afterDelay:2];
    } else {

        [[DataCenter sharedInstance] getDataForFile:file 
                                             server:server 
                                         repository:repository 
                                            isThumb:NO
                                         completion:^(NSData *data) {

                                             NSString *filenamePath =[NSString stringWithFormat:@"temp.%@", [[file path] pathExtension]];
                                             NSString *docDir = [DataCenter getDocumentsDirectoryPath];

                                             NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath];

                                             [data writeToFile:fullPath atomically:YES];

                                             [webView loadData:data 
                                                      MIMEType:[DataCenter mimeTypeForFile:file]
                                              textEncodingName:nil 
                                                       baseURL:nil];

                                             if (!data) {
                                                 [SVProgressHUD dismissWithError:@"Error!"];
                                             } else {
                                                 [SVProgressHUD dismiss];
                                             }

                                         } progress:^(float progress) {
                                             [SVProgressHUD setFloat:progress];
                                         }];


    }

}

- (void)dealloc {
    [buttonNext release];
    [buttonBack release];
    //[webView release]; ---CRASH HAPPENS IF I UNCOMMENT THIS LINE
    [super dealloc];
}

@end

该应用程序在[super dealloc];崩溃。我有NSZombieEnabled,它没有给我任何关于“发送到解除分配的实例的错误....”的错误。我发现的是,如果我评论[webView release],,那么崩溃就不会发生。 我检查,我只分配一次UIWebView并释放一次。

更新请在下面找到完整的课程。正如我所说,那个课程中有很多不相关的内容,我在其中找不到任何问题,也许你们找到了一些东西:

#import "MediaVC.h"
#import "DataCenter.h"
#import "SVProgressHUD.h"
#import "File.h"

@implementation MediaVC
@synthesize file, repository, server, delegate;

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self 
                                                                                action:@selector(done)];

    UIBarButtonItem *buttonAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                                  target:self 
                                                                                  action:@selector(action:)];


    buttonNext = [[UIBarButtonItem alloc] initWithTitle:@"Next" 
                                                  style:UIBarButtonItemStyleDone 
                                                 target:self 
                                                 action:@selector(next)];

    buttonBack = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                  style:UIBarButtonItemStyleDone 
                                                 target:self 
                                                 action:@selector(back)];

    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                          target:nil 
                                                                          action:nil];


    [[self navigationItem] setLeftBarButtonItem:buttonDone];
    [[self navigationItem] setRightBarButtonItem:buttonAction];
    [[self navigationController] setToolbarHidden:NO];
    [self setToolbarItems:[NSArray arrayWithObjects:buttonBack, flex, buttonNext, nil] animated:YES];



    webView = [[UIWebView alloc] initWithFrame:[[self view] bounds]];
    [webView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [webView setScalesPageToFit:YES];
    [webView setMultipleTouchEnabled:YES];

    [[self view] addSubview:webView];

    [self displayFile];


    [flex release];
    [buttonDone release];
    [buttonAction release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)done {
    [delegate doneMediaVC:self];
}

- (void)action:(id)button {
    NSString *filenamePath = [file path];
    NSString *docDir = [DataCenter getDocumentsDirectoryPath];

    NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath];

    NSURL *url = [NSURL fileURLWithPath:fullPath];
    UIDocumentInteractionController *c = [UIDocumentInteractionController interactionControllerWithURL:url];
    BOOL success = [c presentOpenInMenuFromBarButtonItem:buttonBack animated:YES];
    if (!success) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"An application cannt be found to open this file" 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }
}

- (void)displayFile {

    [self setTitle:[file name]];

    [SVProgressHUD showInView:[[self navigationController] view] 
                       status:@"Loading..." 
             networkIndicator:YES
                     progress:YES];

    NSString *mimeString = [DataCenter mimeTypeForFile:file];


    if (!mimeString) {
        [webView loadData:nil 
                 MIMEType:nil 
         textEncodingName:nil 
                  baseURL:nil];


        [SVProgressHUD dismissWithError:@"Can't display this file." afterDelay:2];
    } else {

        [[DataCenter sharedInstance] getDataForFile:file 
                                             server:server 
                                         repository:repository 
                                            isThumb:NO
                                         completion:^(NSData *data) {

                                             NSString *filenamePath =[NSString stringWithFormat:@"temp.%@", [[file path] pathExtension]];
                                             NSString *docDir = [DataCenter getDocumentsDirectoryPath];

                                             NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath];

                                             [data writeToFile:fullPath atomically:YES];

                                             [webView loadData:data 
                                                      MIMEType:[DataCenter mimeTypeForFile:file]
                                              textEncodingName:nil 
                                                       baseURL:nil];

                                             if (!data) {
                                                 [SVProgressHUD dismissWithError:@"Error!"];
                                             } else {
                                                 [SVProgressHUD dismiss];
                                             }

                                         } progress:^(float progress) {
                                             [SVProgressHUD setFloat:progress];
                                         }];


    }

    if (![delegate canGoNext:self currentFile:file]) {
        [buttonNext setEnabled:NO];
    } else {
        [buttonNext setEnabled:YES];
    }


    if (![delegate canGoPrevious:self currentFile:file]) {
        [buttonBack setEnabled:NO];
    } else {
        [buttonBack setEnabled:YES];
    }
}

- (void)back {
    [self setFile:[delegate getPreviousFileForMediaVC:self currentFile:file]];
    [self displayFile];
}

- (void)next {
    [self setFile:[delegate getNextFileForMediaVC:self currentFile:file]];
    [self displayFile];
}

- (void)dealloc {
    [buttonNext release];
    [buttonBack release];
    //[webView release];
    [super dealloc];
}

@end

谢谢!

1 个答案:

答案 0 :(得分:7)

您是否在文档中看到了这一行?

  

重要说明在发布您拥有的UIWebView实例之前   设置委托,必须先将其委托属性设置为nil。这个   例如,可以在你的dealloc方法中完成。

您的“相关代码”没有显示您设置代理,但是再次,您的相关代码根本没有完成。 (flex?buttonDone?buttonAction?)

如果您想要一个好的答案,请分享所有代码。