I am trying to create a delegate for an NSTextField
in my view controller, but the program crashes with EXC_BAD_ACCESS
. Why does this happen? I read that I am calling a non-existent object, but I don´t know what does not exist. I am using ARC.
This is how the delegate object is created in my view controller:
#import <Cocoa/Cocoa.h>
#import "Delegate.h"
@interface ViewController : NSViewController <NSTextFieldDelegate>{
}
@end
--
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSTextField* textField1 = [[NSTextField alloc] initWithFrame:NSMakeRect(200, 200, 150, 20)];
[self.view addSubview:textField1];
Delegate* delegate1 = [[Delegate alloc]init];
[textField1 setDelegate:delegate1];
}
@end
Why does my program crash?
I think the
delegate1
is release, inviewDidLoad
Delegate* delegate1 = [[Delegate alloc]init];
You should create a var for handling it in
ViewController.h
. Thendelegate1 = [[Delegate alloc]init];