このリンクで説明されているものに基づいたフレームワークに連続したビューをロードする iPhone アプリケーションがあります(基本的にはViewController
、メソッドを使用して追加のビューをロード/削除するメインdisplayView
)。私のアプリケーションでは、NIB を使用しています(例のリンクではコード化されたビューを使用しています) ViewControllers
。
Instruments でのデバッグではリークは表示されませんが、セクション (View.xib を含む ViewController) に出入りすると、nib はメモリ内に残るため、数回のイン/アウトの後、メモリが蓄積し始めます。
ペン先がアンロードされていないことはわかっています。これは、ほとんどプログラムで作成されたもの (IB には何もない) と、IB で作成された画像とボタンがあるためです。大きい方が最初にロードされ、小さい方が次にロードされます。インスツルメントへの割り当てが減少することが予想されます。
どうすればこれを防ぐことができますか?
私の構造は次のとおりです。以下にいくつかのコメントがあります。
`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
case
私が持っている「詳細」 と同じ数がありViewControllers
ます(現在は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];
}
に注目しNSLog
てdealloc
ください。これは呼び出されています (コンソールで確認できます) が、nib に必要なメモリが解放されていません (新しい nib がロードされるため、Instruments はセクションを離れるときにメモリ割り当ての増加を示します)。
どんな助けでも大歓迎です。何百万もの異なることを試しましたが、ペン先をアンロードできません。