我将 UINavigationController 和 UITableview 子类化,出于某种原因,我的视图正在泄漏内存,尽管我已经实现了所有正确的方法和释放调用。当我使用本机类而不是子类时,一切正常,没有泄漏。
编辑:
这是我的超类标题:
//
// MBAbstractViewController.h
// GabbaiHD
//
// Created by Moshe Berman on 11/24/10.
// Copyright 2010 MosheBerman.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MBAbstractViewController : UIViewController {
IBOutlet UIImageView *backgroundImageView;
NSString *announcementText;
}
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSDictionary *options;
@property (nonatomic, retain) NSString *announcementText;
-(void) setAnnouncementText:(NSString *)text;
@end
超类实现:
//
// MBAbstractViewController.m
// GabbaiHD
//
// Created by Moshe Berman on 11/24/10.
// Copyright 2010 MosheBerman.com. All rights reserved.
//
#import "MBAbstractViewController.h"
@implementation MBAbstractViewController
@synthesize type, options, announcementText;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// 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];
UIColor *clearColor = [[UIColor alloc] colorWithAlphaComponent:0.0];
[self.view setBackgroundColor: clearColor];
[clearColor release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
return YES;
}else{
return NO;
}
}
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[announcementText release];
[options release];
[type release];
[super dealloc];
}
@end
这是我的子类标题:
//
// MBAnnouncementViewController.h
// GabbaiHD
//
// Created by Moshe Berman on 11/24/10.
// Copyright 2010 MosheBerman.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MBAbstractViewController.h"
@interface MBAnnouncementViewController : MBAbstractViewController {
IBOutlet UILabel *announcement;
}
- (void) setAnnouncementText:(NSString *)text withSize:(CGFloat)size;
@end
和子类实现:
//
// MBAnnouncementViewController.m
// GabbaiHD
//
// Created by Moshe Berman on 11/24/10.
// Copyright 2010 MosheBerman.com. All rights reserved.
//
#import "MBAnnouncementViewController.h"
#import "Constants.h"[
@implementation MBAnnouncementViewController
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[announcement setText:announcementText];
UIImage *slideImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[NSString stringWithFormat:@"%@_slide", kTheme]description] ofType:@"png"]];
[backgroundImageView setImage:slideImage];
[slideImage release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (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.
}
- (void) setAnnouncementText:(NSString *)text withSize:(CGFloat)size{
UIFont *font = [[UIFont alloc] fontWithSize:size];
[announcement setFont:font];
[font release];
[announcement setText:text];
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[announcementText release];
[super dealloc];
}
@end
什么可能导致子类中的内存泄漏?我错过了什么吗?(这里有更多相关代码。)