-1

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
Robby
  • 23
  • 8

1 Answers1

1

I think the delegate1 is release, in viewDidLoad

Delegate* delegate1 = [[Delegate alloc]init];

You should create a var for handling it in ViewController.h. Then

delegate1 = [[Delegate alloc]init];

tuledev
  • 10,177
  • 4
  • 29
  • 49
  • I created in the `Viewcontroller.h` an instance of my delegate: `Delegate* delegate;` and I deleted the `Delegate* delegate1 = [[Delegate alloc]init];` in the `Viewcontroller.m`. Its still doesn´t print "ABC" in the console ;(. But this time it doesn´t crash ;) – Robby Aug 25 '15 at 13:39
  • Don't delete it. Try my answer please. Replace with `delegate1 = [[Delegate alloc]init];` – tuledev Aug 25 '15 at 14:00
  • Thank you very much ! It works now. But why do I have to write the `delegate1` one time in `interface` and in `implementation` of the `ViewController` ? – Robby Aug 25 '15 at 19:57
  • I recognized that it prints "ABC" into the console, but doesn´t create the textfield. Why does the delegate method not create a new textfield ? – Robby Aug 25 '15 at 21:08
  • Because if you don't have any pointer to handle `delegate1`. It will be released. For the textfield, check IBOutlet window1, and frame of window1 vs textfield – tuledev Aug 26 '15 at 01:25
  • I don´t understand your help. Can you give me some code example ? – Robby Aug 26 '15 at 09:29
  • Check in InterfaceBuilder. And try to log `window1.frame`. Maybe textfield go out side the window. Or you did connect window1 with InterfaceBuilder – tuledev Aug 26 '15 at 09:41
  • Its still doesn´t work. How should I log `window1.frame` ? – Robby Aug 26 '15 at 10:47
  • Use `NSLog` to print to cmd. http://stackoverflow.com/questions/5486270/using-nslog-for-debugging – tuledev Aug 26 '15 at 11:53
  • Thank you ! I added `NSLog(@"%@",[NSValue valueWithRect:textField2.frame]);` into the `Delegate.m` file. This is what appears on the console: `2015-08-26 15:12:32.747 Delegate_Test[1942:93806] NSRect: {{30, 30}, {170, 20}}`. – Robby Aug 26 '15 at 13:16
  • How about windown1.frame? – tuledev Aug 26 '15 at 13:47
  • Why did you create textfield with `NSMakeRect(300, 300, 170, 20)`, but the log is `{{30, 30}, {170, 20}}`? – tuledev Aug 26 '15 at 13:48
  • Oh, I was playing around with the values. Now it logs `textField2: NSRect: {{300, 300}, {170, 20}}`. When I write `NSLog(@"%@",[NSValue valueWithRect:window1.frame]);` into the `Delegate.m` my programm logs: `NSLog(@"%@",[NSValue valueWithRect:window1.frame]);`. Event when I write into the code: `[window1 setFrame:NSMakeRect(0, 0, 800, 800)];` it logs `2015-08-26 18:10:56.355 Delegate_Test[2364:134180] NSRect: {{0, 0}, {0, 0}}` – Robby Aug 26 '15 at 16:12
  • See, the problem is `window1.frame`. Check the connection of window1 (with IB). Did you connect it? – tuledev Aug 26 '15 at 16:17
  • Yes, I connected it. I think I made something little wrong and it drives me crazy. It still doesn´t work. – Robby Aug 26 '15 at 16:40
  • Do you use auto-layout? Or you can search window1 to see what you did with it. – tuledev Aug 26 '15 at 16:43
  • Yes, but even with turning auto-layout off it doesn´t work. – Robby Aug 26 '15 at 16:50
  • Do you see the window1 is displayed? – tuledev Aug 27 '15 at 03:15
  • No, but I don´t know how to solve it. Perhaps I can upload my project and you can check it ? – Robby Aug 27 '15 at 10:52