应用程序启动后,自定义类将解除分配

时间:2010-06-09 10:00:05

标签: objective-c dealloc

我已经在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 App [91100:207] *** - [control] performSelector:withObject:withObject:]:发送到解除分配的实例的消息0x3843d80

有没有人有任何想法?

2 个答案:

答案 0 :(得分:0)

尝试将control.h更改为:

#import <Foundation/Foundation.h>
@interface control : NSObject 
{
    UILabel *PlayerScore;
}
@property (nonatomic, retain) IBOutlet UILabel *PlayerScore;
-(IBAction) addPoint: sender;
-(void) dealloc;
@end

然后将@synthesize添加到'control'的实现部分。

另外,谁引用了NIB中的控件类?

答案 1 :(得分:0)

尝试从Interface Builder中删除ViewController,并将文件所有者用作“控件”。

首先,将@interface control : NSObject更改为@interface control : UIViewController

什么改变?


啊,你为什么用

-(IBAction) addPoint: sender;

代替

-(IBAction) addPoint:(id) sender;