0

I used this code to show window controller window in modal view. I have actions in window controller for buttons in window. And when i click the button the app crashed and get no error message. Am I wrong in setting the delegate ?

[[NSApplication sharedApplication] beginSheet:windowObj.window modalForWindow:self.view.window modalDelegate:windowObj didEndSelector:nil contextInfo:nil];
prabhu
  • 1,158
  • 1
  • 12
  • 27
  • Add an exception breakpoint and retry. We need a stacktrace in order to help... – trojanfoe Jul 16 '14 at 06:42
  • I tried. I don't get any exception , it stops in 'return NSApplicationMain(argc, (const char **)argv);' line with EXC_BAD_ACCESS (code=EXC_1386_GPFLT) – prabhu Jul 16 '14 at 06:53
  • You should be able to trap the root-cause of that exception... – trojanfoe Jul 16 '14 at 06:54
  • I think we could use some more code relating to the existence of `windowObj` and `view` – Alex Zielenski Jul 16 '14 at 06:55
  • @trojanfoe No i couldn't. That is the major issue for cant fix it. – prabhu Jul 16 '14 at 06:59
  • Well please pursue solutions to try and catch the exception at the place that it occurs; as mentioned before we cannot help without a stacktrace. – trojanfoe Jul 16 '14 at 07:00
  • @AlexZielenski `windowController *windowObj = [[windowController alloc] initWithWindowNibName:@"windowController"]; [[NSApplication sharedApplication] beginSheet:wwindowObj.window modalForWindow:self.view.window modalDelegate:windowObj didEndSelector:nil contextInfo:nil];` – prabhu Jul 16 '14 at 07:21

1 Answers1

1

windowObj is being deallocated (probably by ARC if you are using it) before the sheet can display the window. You should add a strong property to whatever class you are using for this and set the windowObj to it like so:

@property (strong) MyWindowController *windowObj;
...
// Instantiate the window controller
self.windowObj = [[windowController alloc] initWithWindowNibName:@"windowController"];
[[NSApplication sharedApplication] beginSheet:windowObj.window modalForWindow:self.view.window modalDelegate:windowObj didEndSelector:nil contextInfo:nil]; 
Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44
  • The problem is windowObj is being deallocated. No need for `[self.windowObj loadWindow];` I added strong property and it is working. Thank you. – prabhu Jul 16 '14 at 07:38
  • Usinf `[self.windowObj loadWindow];` intercepts window delegates which leads to another issue. Its better to avoid such coding in this perspective. – prabhu Jul 16 '14 at 10:03