我是iOS平台的新手,我在内存管理方面遇到了一些问题。我正在通过自定义UIViewController类的初始化程序传递一个对象(一个Trial),当UIViewController最终收到它时,该对象为nil。我希望有人可以指出正确的方向。我在下面包含了一些源代码。
试用版
@interface Trial : NSObject {
NSString *IRBNumber;
NSString *PI;
NSString *Sponsor;
NSString *ContactName;
NSString *ContactPhone;
NSString *ContactEmail;
NSString *Location;
NSString *Objective;
NSString *Eligibility;
NSString *Name;
NSString *DiseaseGroup;
NSString *Age;
}
@property (retain, nonatomic) NSString *IRBNumber;
@property (retain, nonatomic) NSString *PI;
@property (retain, nonatomic) NSString *Sponsor;
@property (retain, nonatomic) NSString *ContactName;
@property (retain, nonatomic) NSString *ContactEmail;
@property (retain, nonatomic) NSString *ContactPhone;
@property (retain, nonatomic) NSString *Location;
@property (retain, nonatomic) NSString *Objective;
@property (retain, nonatomic) NSString *Eligibility;
@property (retain, nonatomic) NSString *Name;
@property (retain, nonatomic) NSString *DiseaseGroup;
@property (retain, nonatomic) NSString *Age;
@end
DiseaseControllersViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Trial *trial = (Trial *)[dataArray objectAtIndex:indexPath.row];
TrialDetailController *detailViewController = [[TrialDetailController alloc] initWithNibNameAndTrial:@"TrialDetailController" bundle:nil trial:trial];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
[trial release];
}
这是初始化器的定义
-(id)initWithNibNameAndTrial: (NSString*)NibNameOrNil bundle:(NSBundle*)nibBundlerOrNil trial:(Trial *)inTrial {
self = [super initWithNibName:NibNameOrNil bundle:nibBundlerOrNil];
if(self) {
self.trial = inTrial;
}
return self;
}
TrialDetailController.h
#import "Trial.h"
@interface TrialDetailController : UITabBarController {
Trial *trial;
}
@property (nonatomic, retain) Trial *trial;
-(id)initWithNibNameAndTrial: (NSString*)NibNameOrNil bundle:(NSBundle*)nibBundlerOrNil trial:(Trial *)inTrial;
-(IBAction)objectiveTabItemClick:(id)sender;
-(IBAction)detailsTabItemClick:(id)sender;
@end
最佳答案
您不应在初始化方法中使用self.trial。设为trial = [inTrial retain];
。
然后,您不应在TrialDetailController.h中导入Trial.h,而应在TrialDetailController.m中进行导入。在TrialDetailController.h中的@interface行上方放置@class Trial;
(正向定义)。