2012-06-15 57 views
0

我試圖找出爲什麼我的NSTextFields只保留在第一個方法sendVarsToButton但不是在updateTotal方法。我需要從第一個方法中的TextField集合中訪問值,但是我不能這樣做,因爲看起來像我的IBOutlets在sendVarsToButton方法之後釋放自己。你能幫我嗎!?兩個類之間的IBOutlets屬性不保留。釋放?

這裏是我的.h

#import <Cocoa/Cocoa.h> 
#import "TransactionViewController.h" 
@class TransactionButtonModel; 

@interface TransactionButtonController : TransactionViewController 

{ 
NSMutableArray *buttonsArrays; 
TransactionViewController *transactionViewController; 
TransactionButtonModel *transactionButtonModel; 
} 
@property(nonatomic,retain) IBOutlet NSTextField *nom; 
@property(nonatomic,retain) IBOutlet NSTextField *descriptionText; 
@property(nonatomic,retain) IBOutlet NSTextField *prix; 
@property(nonatomic,retain) IBOutlet NSTextField *CPUField; 
@property(nonatomic,retain) IBOutlet NSTextField *quantite; 
@property(nonatomic,retain) IBOutlet NSTextField *total; 

-(void)sendVarsToButton:(NSString *)name:(NSString *)description:(double)price:(double)CPU:(long)tag; 
-(void)updateTotal:(int)newQuantity; 
-(void)addQuantiteToExistingProduct:(long)tag; 
-(IBAction)removeProductFromView:(id)sender; 

這裏是我的.m

#import "TransactionButtonController.h" 
#import "TransactionViewController.h" 
#import "TransactionButtonModel.h" 

@implementation TransactionButtonController 
@synthesize prix; 
@synthesize nom; 
@synthesize descriptionText; 
@synthesize CPUField; 
@synthesize total; 
@synthesize quantite; 

//In this method, everything works fine 

-(void)sendVarsToButton:(NSString *)name :(NSString *)description :(double)price :(double)CPU:(long)tag 
{ 
[nom setTag:tag]; 
[descriptionText setTag:tag]; 
[prix setTag:tag]; 
[CPUField setTag:tag]; 
[quantite setTag:tag]; 
[total setTag:tag]; 

nom.stringValue = name; 
descriptionText.stringValue = description; 
[prix setDoubleValue : price]; 
CPUField.doubleValue = CPU; 

total.doubleValue = [TransactionButtonModel calculateButtonTotal:quantite.intValue :prix.doubleValue]; 

NSLog(@"retain! :%lu",[[prix viewWithTag:tag] retainCount]); // returns 2 

[transactionButtonModel release]; 

} 
-(void)updateTotal:(int)newQuantity 
{ 
NSLog(@"retain! :%lu",[[prix viewWithTag:2] retainCount]); //returns 0 
[total setDoubleValue:[TransactionButtonModel calculateButtonTotal:newQuantity :prix.doubleValue]]; // value of prix = 0 and prix = null 
NSLog(@"Updated! :%i",newQuantity); 
} 

-(void)dealloc 
{ 
[nom release]; 
[quantite release]; 
[prix release]; 
[total release]; 
[descriptionText release]; 
} 

在此先感謝。

+0

'retainCount'不符合您的期望。 – 2012-06-15 17:35:08

+0

你是什麼意思?當我做'[prix description]'時,它返回'null'。所以我的文本字段沒有分配。 –

+0

是的,你是對的:它沒有被分配。它不是*釋放*雖然。如果它是一個已經死亡的釋放對象,它將是一個無效的非空指針(廢話)。你的物體還沒有誕生。 – 2012-06-15 17:41:21

回答

1

這聽起來像你在這裏有幾個問題。在沒有特定的順序:

  1. 你釋放在sendVarsToButton:::::transactionButtonModel即使你不存在創建它,有沒有你要特別明顯的原因。這似乎可能是內存管理錯誤,但沒有上下文很難說。

  2. 您正在尋找引用計數機制來查看爲什麼變量爲null。過度釋放對象不會將引用該對象的變量設置爲空 - 它只是一個垃圾指針,可能會導致程序崩潰。一個變量意外爲空的最可能的原因是a)以不同於你期望的順序運行的方法,或者b)你有兩個不同的類實例,你認爲它們是同一個實例。在這種情況下,我的錢就在B上。你可能創建了這個類的兩個實例,其中一個實際上顯示了視圖,另一個實質上是「空白的」。嘗試在兩個方法中記錄self並查看它是否是同一個對象。

  3. 在一種方法你登錄viewWithTag:tag和其他你登錄viewWithTag:2 - 它不一定是一個安全的假設,即tag爲2

  4. prix是的NSTextField - 你爲什麼要問它子視圖? NSTextField通常不會有任何有用的子視圖。這種設計似乎有些奇怪。

  5. 您的方法名稱與所有這些裸冒號是堅果。這很難閱讀,而且往往會導致錯誤(由於錯誤閱讀代碼的可能性,當你不是「現在」時,以及它違反了語言的習慣用法的事實)。

  6. 您正在根據retainCount來跟蹤內存管理。由retainCount返回的值最多也是有問題的,而且往往是徹頭徹尾的欺騙性,因爲所有事情都會被保留並自動釋放,retainCount將不會爲您提供足夠的信息來解決這個問題。如果您有內存管理問題(其中不是似乎是您的變量變爲零的情況下),一個好方法是使用儀器和調試器來追蹤它,而不是隨機retainCount日誌記錄。

+0

+1!謝謝!的確,我每次點擊按鈕時都會在這個類上調用alloc。所以對象被重新創建。現在我需要找出一種方法,使用NSViewController的initWithNibNamed從1個分離的nib文件創建多個子視圖,而不必多次調用alloc和initWithNibNamed! –