0

我试图弄清楚为什么我NSTextFields只保留在第一种方法sendVarsToButton中而不是updateTotal方法中。我需要从第一种方法中设置的 TextField 访问值,但我不能,因为我的 IBOutlets 似乎在sendVarsToButton方法之后自行释放。你能帮我吗!?

这是我的.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];
}

提前致谢。

4

1 回答 1

1

听起来您在这里遇到了一些问题。没有特别的顺序:

  1. transactionButtonModel即使你没有在那里创建它,你也会发布sendVarsToButton:::::它,并且没有特别明显的理由你想要这样做。这似乎可能是一个内存管理错误,但如果没有上下文就很难说。

  2. 您正在查看引用计数机制以了解变量为空的原因。过度释放一个对象不会将引用该对象的变量设置为 null ——它只是一个垃圾指针,可能会使你的程序崩溃。变量意外为 null 的最可能原因是 a) 方法运行的顺序与您预期的不同,或者 b) 有两个不同的类实例,您将它们视为同一个实例。在这种情况下,我的钱会花在 B 上。您可能正在创建此类的两个实例,其中一个实际显示视图,另一个实质上是“空白”。尝试登录self这两种方法,看看它是否是同一个对象。

  3. 在一种方法中,您正在记录viewWithTag:tag,而在另一种方法中,您正在记录viewWithTag:2——这不一定是一个安全的假设,tag即 2。

  4. prix是一个 NSTextField - 你为什么要它的子视图?NSTextField 通常不会有任何有用的子视图。这个设计似乎有些奇怪。

  5. 您的方法名称与所有这些裸冒号都很疯狂。它很难阅读,并且经常导致错误(因为当你不在“当下”时可能会误读代码,并且它违反了语言的习惯用法)。

  6. 您依赖于retainCount跟踪内存管理。返回的值retainCount充其量是有问题的,而且通常是彻头彻尾的欺骗性,因为事物一直被保留和自动释放,并且retainCount不会为您提供足够的信息来解释这一点。如果您有内存管理问题(变量变为 nil 时似乎不是这种情况),一个好的方法是使用 Instruments 和调试器来跟踪它,而不是随机retainCount记录。

于 2012-06-15T17:51:05.647 回答