iOS初始化程序参数为nil

时间:2011-12-09 15:45:31

标签: iphone objective-c ios mobile

我是iOS平台的新手,我在内存管理方面遇到了一些问题。我通过自定义UIViewController类的初始化器传递一个对象(一个Trial),当UIViewController最终收到它时,该对象为nil。我希望有人能指出我正确的方向。我在下面提供了一些源代码。

Trial.h

@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

2 个答案:

答案 0 :(得分:0)

你不应该在init-method中使用self.trial。制作trial = [inTrial retain];

然后你不应该在TrialDetailController.h中导入Trial.h,在TrialDetailController.m中执行。在TrialDetailController.h中的@interface行上方放置@class Trial;(前向定义)。

答案 1 :(得分:0)

我的猜测是你的代码 Trial *trial = (Trial *)[dataArray objectAtIndex:indexPath.row];

变量dataArray为零。那么trial也将是零。

{...}}不应该像从善如流所暗示的那样存在。

我找不到任何其他问题。

只有您不必声明属性变量: 您界面中的[trial release];。合成时会自动创建一个变量(我希望你在某处有Trial *trial)。