我有一个iPhone应用程序,它基于此链接中所述的框架,根据这个框架,一个主要的
在Instruments中调试显示没有泄漏,但如果我进入/离开一个部分(带有View.xib的ViewController),则nib文件仍会保留在内存中,因此在几次进入和退出后,内存开始积累。
我知道nib文件没有被卸载,因为其中一个是几乎以编程方式创建的(IB中没有任何内容),而另一个具有在IB中创建的图像和按钮。大的先加载,小的后加载。您应该期望在Instruments中减少分配。
如何防止这种情况发生?
我的结构如下,下面有一些注释:
ViewController
通过displayView
方法加载/移除其他视图。然而,在我的应用程序中,我使用NIBs(示例链接使用编码视图),因此每个ViewController
都有其相应的nib文件。在Instruments中调试显示没有泄漏,但如果我进入/离开一个部分(带有View.xib的ViewController),则nib文件仍会保留在内存中,因此在几次进入和退出后,内存开始积累。
我知道nib文件没有被卸载,因为其中一个是几乎以编程方式创建的(IB中没有任何内容),而另一个具有在IB中创建的图像和按钮。大的先加载,小的后加载。您应该期望在Instruments中减少分配。
如何防止这种情况发生?
我的结构如下,下面有一些注释:
`MyAppDelegate.h`
#import <UIKit/UIKit.h>
@class RootViewController;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *viewController;
-(void) displayView:(int)intNewView;
@end
`MyAppDelegate.m`
#import "MyAppDelegate.h"
#import "RootViewController.h"
@implementation MyAppDelegate
@synthesize window;
@synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
-(void) displayView:(int)intNewView {
[viewController displayView:intNewView];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
这个控制器处理子视图的加载和移除:
`RootViewController.h`
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController {
}
- (void) displayView:(int)intNewView;
@end
`RootViewController.m`
#import "RootViewController.h"
#import "ViewController.h"
@implementation RootViewController
UIViewController *currentView;
- (void) displayView:(int)intNewView {
NSLog(@"%i", intNewView);
[currentView.view removeFromSuperview];
[currentView release];
switch (intNewView) {
case 1:
currentView = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
break;
}
[self.view addSubview:currentView.view];
}
- (void)viewDidLoad {
currentView = [[ViewController alloc]
initWithNibName:@"View" bundle:nil];
[self.view addSubview:currentView.view];
[super viewDidLoad];
}
- (void)dealloc {
[currentView release];
[super dealloc];
}
@end
我有多个“详细信息”ViewControllers
,就会有多个case
(现在有3个case
,但这将增加到10个或更多)。这种结构的目的是轻松地从应用程序的一个“部分”移动到另一个部分(NavBar控制器或TabBar控制器不适合我的特定需求)。
`ViewController.h`
// Generic View Controller Example
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UIImageView *_image1;
UIImageView *_image2;
NSTimer *_theTimer;
}
@property (nonatomic, retain) IBOutlet UIImageView *image1;
@property (nonatomic, retain) IBOutlet UIImageView *image2;
@property (nonatomic, retain) NSTimer *theTimer;
@end
`ViewController.m`
#import "ViewController.h"
#import "MyAppDelegate.h"
@synthesize image1 = _image1, image2 = _image2, theTimer = _theTimer;
- (void)loadMenu {
[self.theTimer invalidate];
self.theTimer = nil;
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:2];
}
-(void)setView:(UIView*)aView {
if (!aView){
self.image1 = nil;
self.image2 = nil;
}
[super setView:aView];
}
- (void)viewDidLoad {
//some code
[super viewDidLoad];
}
- (void)viewDidUnload {
self.image1 = nil;
self.image2 = nil;
}
- (void)dealloc {
NSLog(@"dealloc called");
[self.theTimer invalidate];
[self.theTimer release];
[self.image1 release];
[self.image2 release];
[super dealloc];
}
请注意在 dealloc
中的 NSLog
。它被调用了(我可以在控制台中看到),但是 nib 所需的内存没有被释放(因为加载了新的 nib,在离开某个部分时,Instruments 显示内存分配增加)。
非常感谢您的任何帮助。我已经尝试了无数种方法,但我无法卸载 nibs。