开发者

Custom class deallocated as soon as the app starts

I've added a class object to the nib file. All connections are made.

But for some reason, the object is deallocated as soon as it's created.

Here's the code:

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

Here is the console log:

[Session started at 2010-06-09 19:47:57 +1000.]

2010-06-09 19:47:58.771 App[91100:207] ZOMGWTF?

And after I click the button which messages addPoint, of course, it crashes.

2010-06-09 19:47:59.703 App[91100:207] *** -[control]开发者_C百科 performSelector:withObject:withObject:]: message sent to deallocated instance 0x3843d80

Does anyone have any ideas?


Try changing control.h to be:

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

And then add a @synthesize to the implementation section of 'control'.

Also, who has reference to the control class in the NIB?


Try to remove from your Interface Builder the ViewController and use File Owner's as "control".

First of this, change @interface control : NSObject to @interface control : UIViewController

change anything?


Ah, why you use

-(IBAction) addPoint: sender;

instead

-(IBAction) addPoint:(id) sender;

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜