2011-12-09 59 views
1

我對iOS平臺相當陌生,並且在內存管理方面遇到了一些問題。我通過一個自定義的UIViewController類的初始化程序傳入一個對象(試用版),當UIViewController最終收到它時,該對象爲零。我希望有人能夠指引我走向正確的方向。我已經在下面列出了一些源代碼。iOS初始化參數爲零

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 
的定義
+0

你可以發佈TrialDetailController的init-Method的代碼嗎? – samsam

+0

你明白了。我剛剛發佈了它。 – Hopdizzle

+0

你試過調試過嗎?您可以在gdb調試器(控制檯窗口)中設置斷點並查看變量/屬性值。請參閱[XCode4中的Objective-C調試技巧?](http://stackoverflow.com/a/7650979/590956)。只是想知道'po dataArray'會在調試器中顯示什麼。你在哪裏添加值到這個數組中,並且在你嘗試向它添加對象之前初始化數組? – Sam

回答

0

你不應該在init方法中使用self.trial。做那trial = [inTrial retain];

然後,您不應該在TrialDetailController.h中導入Trial.h,在TrialDetailController.m中執行該操作。在TrialDetailController.h中的@interface行之上放置一個@class Trial;(一個前向定義)。

+0

爲什麼不在init中使用self.trial?如果你有保留財產,你永遠不應該使用保留/釋放ouside的getter或setter。這就是爲什麼保留物業在那裏! – Sulthan

+0

請參閱http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html段落'不要在初始化方法和dealloc中使用訪問器方法。 –

+0

Init和dealloc方法的行爲與任何其他方法的行爲完全相同。爲什麼不在init/dealloc中調用它們通常是因爲你的設置器在引用另一個尚未分配的屬性或已經釋放的屬性時行爲不正確。但請注意,當您不使用setter時可能會出現同樣的問題。這一切都取決於你的setter的內容,使用保留/釋放不會使它更好。無論如何,你必須考慮釋放屬性的順序。 – Sulthan

0

我的猜測是,當你的代碼做 Trial *trial = (Trial *)[dataArray objectAtIndex:indexPath.row];

變量dataArray是零。那麼trial也是零。

[trial release];不應該在那裏,因爲從善如流是建議。

我找不到任何其他問題。

只有您不必在界面中聲明屬性變量: Trial *trial。合成時會自動創建一個變量(我希望你在某處有@synthesize trial;)。