Dale Myers
Dale Myers

Reputation: 2821

App crashes when button is pressed

I have loaded a new view using the code

UIViewController* newController = [[UIViewController alloc] initWithNibName:@"NFCController" bundle:nil];
[self.view addSubview:newController.view];

and this loads in the new view perfectly. However, I have connected a button in the new view to a class so that I can use the press event to do things. However, even though the method is still only:

- (IBAction)donePress:(id)sender {}

the app still crashes. I get the error EXC_BAD_ACCESS on the main method in main.m.

Has anyone got any ideas what could be causing this? I can provide more information on request.

Thanks.

Edit: This happens with all controls that are connected to methods.

Upvotes: 1

Views: 615

Answers (2)

Farrukh Javeid
Farrukh Javeid

Reputation: 634

Try if you are putting the right name of the nib file and also if the nib file answers to your controller class.

Upvotes: 2

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

your controller is getting released as it is not retained anywhere leading to crash. only your controller's view is being retained.

Keeping your controller retained like with

//declaring newController as (strong) in .h file and use
self.newController = [[UIViewController alloc] initWithNibName:@"NFCController" bundle:nil];

[self.view addSubview:self.newController.view];

Now your controller is also being retained. Your code will work fine now.

Upvotes: 5

Related Questions