2

I am creating a simple app that let's the user view movies after clicking buttons. The first screen you see is the Main Menu with 2 buttons. One of them sends the user to a sub view which I coded as this in an IBAction method:

ViewConroller *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];
[self.view addSubview:[nextView view]];
[nextView release];

This part is fine except that the sub view shows a 20 px bar across the bottom of the previous view. I looked around and none of the solutions like this one work for me. This isn't even the main problem. On the sub view, whenever I click a button, the app explodes. The IBAction methods break before even executing a line inside the method. Here, in the goBack IBAction method, I am trying to dismiss the subview and go back to the main menu:

[self.view removeFromSuperview];

My connections are correct in my XIB files and everything seems to be in order. The buttons just aren't executing. I'm not sure if it has to do with adding the sub view or what. I have done a lot of searching around on google and this website, but I still can't find a solution.

Here is an example of the error I'm getting:

Terminating app due to uncaught exception 'NSInvalidArgumentException, reason: '-[__NSCFType goBack:]: unrecognized selector sent to instance 0x5640b70'

Hopefully you guys can help me out, this is really quite frustrating. I feel like I've tried everything. Thanks for your time. Let me know if I need to provide any more information. Thanks again.

Community
  • 1
  • 1
Link
  • 396
  • 1
  • 4
  • 15
  • You are releasing your viewController but keep the viei around. this will give you problems later on. I would suggest the either use presentenModalViewController method or swith to UINavigationController. – rckoenes Mar 16 '11 at 16:49
  • I tried using the ModalViewController and was still unable to get the buttons to do anything but crash my app. Any ideas? – Link Mar 16 '11 at 17:13

1 Answers1

3
ViewController *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];

View controller is created.

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

self.view added value of nextView.view as it's subview and retained it (N.B. it retained next.view value not nextView itself)

[nextView release];

nextView is released and immediately deallocated (because it has never been retained by anybody)

Button is trying to send a message to deallocated object -> behavior is undefined, in your case some other object (or something that looks like an object) is there and it doesn't understand what goBack: means.

That's why you should not invent a wheel (especially if you are new to this) with your own navigation. Use UINavigationController instead.

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
hoha
  • 4,418
  • 17
  • 15
  • Thank you for your help. I am still running into the same issue with a reason '--[ViewController goBack:]: unrecognized selector send to instance. I tried using a ModalViewController as well and my IBAction methods are still not executing. – Link Mar 16 '11 at 17:11
  • Without your new code and error messages you get it's hard to tell what's wrong. – hoha Mar 16 '11 at 17:15
  • Sorry about that. I just removed the [nextView release]; line so that it wouldn't be deallocated. Kept everything else the same. The only thing that changes is the new error code of "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__ViewController goBack:]: unrecognized selector sent to instance 0x561df30" – Link Mar 16 '11 at 17:20
  • I see. Are you sure that `ViewConroller` has implementation of `goBack:` method? Runtime can't find it. – hoha Mar 16 '11 at 17:28
  • I put the goBack method inside my NextMenuController. Should it be in my main view controller as well? Or only in the main view controller? – Link Mar 16 '11 at 17:33
  • Ok I got it. Thank you very much for your help. I was creating new classes for each of my menus, when really all I had to do was just create a new XIB file and present it as a ModalViewController. – Link Mar 16 '11 at 17:50