0

I have a subView of the rootViewController and when called, buttons cannot be pressed on it without the error:

 Thread 1: EXC_BAD_ACCESS

Here is the important code:

AppDelegate:

MainViewController *MainVC = [[MainViewController alloc]init] ;
self.window.rootViewController = mainVC ;

And in the MainViewController class:

OtherViewController *SomeView = [[OtherViewController alloc]init] ;
[self.view addSubview:SomeView.view] ;

The OtherViewController xib loads as it should (the viewDidLoad stuff executes). However, when I try to press one of the buttons in the simulator (on the OtherViewController) I get the above error.

And the code of the button:

IBOutlet UIImageView *Image; //in OtherViewController.h

//in OtherViewController.m
- (IBAction)Button:(id)sender {
Image.center = CGPointMake(Image.center.x + 1, Image.center.y) ;
}

There are breakpoints throughout OtherViewController.h and .m (everywhere to do with the button). None are triggered. The program breaks and gives the error in AppDelegate: return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

Am I not setting up the second view properly so that it is fully-functional? How do I rid my program of this error?

  • What's the target on the button? – Simon Germain Jul 25 '14 at 01:59
  • No, if you create a button, there's either an `IBAction` attached to it or there's a `[button addTarget:action:forControlEvents:]` call that attaches a selector to the button. – Simon Germain Jul 25 '14 at 02:04
  • Can you post the code within that IBAction? – Simon Germain Jul 25 '14 at 02:08
  • 1
    You can add an exception breakpoint so you can see the exact line it crashes on (helpful if the crash is inside the IBAction). – rebello95 Jul 25 '14 at 02:13
  • There are breakpoints throughout OtherViewController.h and .m (everywhere to do with the button). None are triggered. The program breaks and gives the error in AppDelegate: `return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));` @rebello95 –  Jul 25 '14 at 02:17
  • I think you put normal breakpoints, not exception breakpoints. See Guru's answer on this post: [link](http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode) – rebello95 Jul 25 '14 at 02:21

1 Answers1

1

Your problem is that you create a one-off instance of OtherViewController in your MainViewController. At the end of the scope, ARC releases your instance of OtherViewController.

Here's your solution:

in MainViewController.m

@interface MainViewController() // This is an interface extension, stick that before your @implementation MainViewController

@property (nonatomic, strong) OtherViewContontroller *SomeView;
@end

In your method that creates the OtherViewController, replace:

OtherViewController *SomeView = [[OtherViewController alloc]init] ;
[self.view addSubview:SomeView.view] ;

with this:

self.SomeView = [OtherViewController new]; // <-- new is a shortcut for alloc/init. :)
[self.view addSubview:self.SomeView.view];
Simon Germain
  • 6,834
  • 1
  • 27
  • 42