【发布时间】:2011-11-04 18:33:20
【问题描述】:
我在一个应用中有 7 个 ViewController,其中一个将本地 PDF 加载到 UIWebView 中。初始加载没有问题,但如果我离开那个 ViewController 并返回它,第二次或第三次我在 iPad 上调试时收到“收到内存警告”(我在模拟器中没有遇到这个问题,但是这并不意味着我最终不会得到它)。我已确保大小写/大小写与文件和我的代码一致,并且我没有收到任何构建错误或警告。我被告知有关 xcode4 需要 @property/@synthesize/dealloc 的混杂的东西,所以我尝试了使用和不使用它,结果没有改变。
这是我的 .h 文件
#import <UIKit/UIKit.h>
@interface Article6 : UIViewController {
IBOutlet UIWebView *webview;
}
@end
这是我的 .m 文件
#import "Article6.h"
@implementation Article6
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"mfa" ofType:@"pdf" inDirectory:NO];
NSLog(@"path: %@",path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
@end
我正在使用情节提要中的滑动手势,以及一个常规按钮,这两个 segues 都会出现此错误。是的,所有插座都已连接。
【问题讨论】:
-
每次转场时,您似乎都在加载新的 Article6。你的 NSLog(@"path: %@", path);每次切换到 pfd 时都要记录?
-
是的,NSLOG 记录每个 segue。如果每次加载都是问题,我如何让它不每次都加载?而且我的其他 ViewController(使用 image/scrollview 而不是 pdf/webview)没有出现内存错误。
-
每次都可以加载,只要确保在离开 Article6 视图控制器时释放所有资源。
-
我试过 -(void) dealloc { [webview release];} 但什么也没做......那我还应该尝试什么?
-
您认为 viewcontroller 可能没有被释放。您可以使用日志语句验证 dealloc 是否运行。您还可以在 -(void)viewDidDisappear:(BOOL)animated 或 -(void)viewDidUnload 时释放资源。只需在这些中添加日志语句,以便您更好地了解正在发生的事情。