自定义类在应用程序启动后立即释放
问题描述:
我已经将类对象添加到了nib文件中。所有连接都已完成。自定义类在应用程序启动后立即释放
但由于某种原因,只要创建对象就会释放该对象。
下面的代码:
control.h:
#import <Foundation/Foundation.h>
@interface control : NSObject
{
IBOutlet UILabel *PlayerScore;
}
-(IBAction) addPoint: sender;
-(void) dealloc;
@end
control.m:
#import "control.h"
@implementation control
-(IBAction)addPoint: sender {
NSLog(@"Ohhai. Didn't crash."); //Doesn't even make it to this stage.
int i = [PlayerScore.text intValue];
PlayerScore.text=[NSString stringWithFormat: @"%d",++i];
}
-(void) dealloc {
NSLog(@"ZOMGWTF?");
[super dealloc];
}
@end
这里是控制台日志:
[会话开始于2010-06-09 19:47:57 +1000。]
2010-06-09 19:47:58.771 App [91100:207] ZOMGWTF?
而我点击按钮后,addPoint的消息当然会崩溃。
2010-06-09 19:47:59.703应用[91100:207] *** - [控制] performSelector:withObject:withObject:]:消息发送到释放的实例0x3843d80
不有人有主意吗?
答
尝试改变control.h是:
#import <Foundation/Foundation.h>
@interface control : NSObject
{
UILabel *PlayerScore;
}
@property (nonatomic, retain) IBOutlet UILabel *PlayerScore;
-(IBAction) addPoint: sender;
-(void) dealloc;
@end
,然后添加一个@synthesize来 '控制' 的实现部分。
另外,谁在NIB中引用了控件类?
答
尝试从您的Interface Builder中移除ViewController并使用File Owner's作为“控制”。
首先,这,更改@interface control : NSObject
到@interface control : UIViewController
改变什么?
啊,为什么你用
-(IBAction) addPoint: sender;
代替
-(IBAction) addPoint:(id) sender;
?
试过,但仍然在运行时解除分配。 控制器没有连接参考插座。 – tangrs 2010-06-09 10:45:25
将引用连接到文件所有者...我假设您正在使用垃圾收集器进行构建。没有提及将GC的“控制”对象持续扫除。 – 2010-06-10 09:57:36