-1

im on OSX, XCode9, Objective C.

I have a viewController layouted in IB. The view contains a button connected to the corresponding viewController

SHTourViewController.h

@property (weak) IBOutlet SHStandardButton *closeButton;
// SHStandardButton is a subclass from NSBUtton.

The view controller gets instantinated by code in another class (i need to instantinate this viewController from other classes cause i need it more than once).

// Get instance of viewController
SHTourViewController* tourViewController = [storyBoard instantiateControllerWithIdentifier:@"tourViewController"]; 

Now in viewDidLoad method of my viewController, i like to connect the buttons action and target:

SHTourViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.closeButton setAction:@selector(closeButtonClicked:)];
    [self.closeButton setTarget:self];
}

- (void)closeButtonClicked:(id)sender {
NSLog(@"CLOSE!");
}
}

When i click the button, the app crashes (Thread 1: EXC_BAD_ACCESS). I can't find the mistake i am doing here.

Any help appreciated.

enter image description here

Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • Please provide the actual full crash report from the console. – matt Oct 19 '17 at 17:17
  • I attached an image. Why -1? – Pat_Morita Oct 19 '17 at 17:20
  • If you set a breakpoint on Objective-C exceptions, Xcode should stop at the actual error rather than NSApplicationMain. That would give you a line of code to work with and a useful stack trace. – Phillip Mills Oct 19 '17 at 17:30
  • Because you're just handwaving; you're not showing the real cause of the problem. Merely saying "it crashes" is not informative; you're making us _guess_, and what's the point of that? – matt Oct 19 '17 at 18:29

1 Answers1

0

You are not supplying enough information about what you're doing. But, as it stands, the fact that fact that you are getting a Bad Access would suggest that some important object has vanished prematurely in a puff of smoke. My guess is that that object is self, and that the problem has to do with code after this line:

SHTourViewController* tourViewController = 
    [storyBoard instantiateControllerWithIdentifier:@"tourViewController"]; 

You are obtaining a completely new instance of this view controller but then you are not getting its view correctly into the view hierarchy and the view controller itself into the view controller hierarchy, so the view controller (I'm guessing) is released.

But you didn't show us the relevant code, so that's just a guess.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you! I did not want to "wave hands" - i just said... any help appreciated -> And thanks to the exception breakpoint and your advice i found the mistake. The viewController got deallocated cause it didn't have a strong reference. Once added, it worked fine. – Pat_Morita Oct 20 '17 at 05:15
  • But that's just what I suggested. If my answer was right, wouldn't it be correct to accept it? – matt Oct 20 '17 at 15:40