6

I have got a single view in my storyboard, which I add to my current view by doing the following :

MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];
             [self.view addSubview:mvc.view];

storyboard

The view appears, but everything I do after it appears, leads to a crash. What am I doing wrong ?

Here is an example when it crashes:

-(IBAction)showUsername:(id)sender{

    [testLabel setText:@"username"];

}

Everything is hooked up in storyboard as well, so falsely linked connections should not cause the problem.

the_critic
  • 12,720
  • 19
  • 67
  • 115

5 Answers5

8

You instantiate a new view controller:

MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];

But you do not retain it. Your view hierarchy is, as soon you added it to another view.

[self.view addSubview:mvc.view];

So when a button is clicked, a message is sent to you IBAction, but your view controller has been released already. To prevent this from happening, retain your mvc variable, for example somewhere in a property.

@property(nonatomic, strong) MainViewController *controller;

self.controller = mvc;
Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • yes thank you, I asked this question again, and got the same answer... The problem has already been solved, but thank you anyway. I am sure this will help others too. – the_critic Feb 07 '12 at 13:36
  • Love you <3 debug console doesn't even give a clue to find this out! – Clad Clad Jul 29 '15 at 09:45
0

enter image description here

I can think all reason before you show log...

Seamus
  • 243
  • 1
  • 12
  • I think it gets released or not it is not properly instantiated... But I do not know how to solve it. I assigned it to my controller class, it should work fine. – the_critic Feb 05 '12 at 16:40
  • @user1066899 you can press ctrl and drag your viewcontroller from ib to your .h file. then all is fine. – Seamus Feb 05 '12 at 16:55
0

Turn NSZombie on in the Product>>Edit Scheme you should get more descriptive Error showing then. Then you can add it.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • Yeah thank you that works fine. Now I finally get the error, and as I assumed, my ViewController gets deallocated `-[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x3fa5a60` – the_critic Feb 05 '12 at 16:54
0

Make sure your method is declared and implemented correctly. Also make sure you have IBOutlet UILabel * testLabel in your .h. The only other problem I can think of other than that is how you hooked it up. Does it only crash when you press the button?

Seany242
  • 373
  • 7
  • 20
0

This line is wrong this will be why you are getting the error.

MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];
         [self.view addSubview:mvc.view];

replace it with this

 MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];
[self presentModalViewController:mvc animated:YES];

In storyboards you are not adding a subview you are doing one of three things presenting a modal, pushing it on to the navigation controller stack or making a custom one of these.

Popeye
  • 11,839
  • 9
  • 58
  • 91