0

I have a simple test app (in OSX) that has a view controller with a button in its view. The button's action method is in the view controller's class, and that IBAction is connected in IB (through File's Owner). When the button is clicked, I get an EXC_BAD_Access error (except occasionally I get -[NSRunLoop buttonClick:] instead). I've read a bunch of posts here on SO having to do with NSViewControllers not being in the responder chain, but also that specifically hooking the action method up in IB should work. The only code I have is this:

In the app delegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    TestController *controller = [[TestController alloc] initWithNibName:@"TestController" bundle:nil];
    [self.window.contentView addSubview:controller.view];
}

And, in the TestController class, just this:

-(IBAction)buttonClick:(id)sender {
    NSLog(@"%@",sender);
}

I have 2 questions. Why is this happening, and where is the correct mvc place to put IBActions for button methods (shouldn't controller classes handle these)?

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • qegal's answer, though written as if you are on iOS, would essentially solve the problem for you. You are putting the view controller that you create into a local variable, and it's being deallocated immediately (by ARC). That means the button is messaging an invalid object when it's pushed. If you create a `strong` property in the app delegate to hold the controller, and assign to that, everything will work fine. The view controller is generally the right place to put `IBAction` methods. – jscs Jun 04 '12 at 17:13

3 Answers3

0

First off, I believe in the App Delegate there is already a property called viewController already defined for you. You should use this code self.viewController = [[TestController alloc] initWithNibName:@"TestController" bundle:nil]; instead of TestController *controller = [[TestController alloc] initWithNibName:@"TestController" bundle:nil];. If that doesn't fix it, then I'm not sure what's wrong. Your code should return the attributes of the button you clicked. I created my own sample project and tested out your code (although my app delegate looked different than yours) and it worked fine.

Second, you should elaborate on your second question "Why is this happening, and where is the correct mvc place to put IBActions for button methods (shouldn't controller classes handle these)?". It's not very clear what you mean.

Hope this helps a little.

pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • No, there is no viewController defined in the app delegate -- this OSX not iOS. As for the second question, I'm just trying to figure out what is the mvc "correct" way to handle action methods from a view's controls. What class should implement those methods? They work if I put them in the subclassed view, but it was my understanding that views shouldn't do this kind of thing, they're only for displaying. – rdelmar Jun 02 '12 at 05:21
  • 1
    You should put the IBActions in the controller of the view you want the IBActions to act on. – pasawaya Jun 02 '12 at 05:43
  • This is precisely what is NOT working. I have the IBActions in my controller of the view. – rdelmar Jun 02 '12 at 05:45
  • Well then there's something messed up. It's supposed to be there. – pasawaya Jun 02 '12 at 21:36
0

NSViewController will not respond to IBActions on mac os x on iPhone It is the correct place to put your IBAction as the view should only draw its content (data) not change it. But on Mac os x the NSViewController is for setting up the NSView it's no ment to respond to IBActions you have two choices one is to put your IBAction in your NSView or is create a NSWindowController . On mac osx you have plenty of screen space and you will alway have views within a window you'll use NSViewController to add them to your window and to setup your view but the window is first in your responder chain then your NSWindowController . eg: you may have one window and two view controller showing the same view which may have 5 test fields in it and view controller one loads that with data but you can not edit the data then view controller two loads the same view with the same data but enable editing for the text fields so. but all action methods will go to the WindowController .

Apps 4 U
  • 239
  • 4
  • 16
0

I recommend you to check your viewController's identity inspector. It may indicate wrong custom class. viewController created basically usually has name ViewController on the section.

Robert
  • 5,278
  • 43
  • 65
  • 115
serendi21
  • 1
  • 2