views:

327

answers:

1

I’m trying to implement a partial overlay modal in my app with the code from “Semi-Modal (Transparent) Dialogs on the iPhone” at ramin.firoozye.com. The overlay functionality works and it slides the modal into view, but calling any IBAction from the modal's controller causes an “Unrecognized Selector Sent to Instance” crash.

I recreated the basic functionality with that code isolated, and it triggers the same error. To see what I’m talking about, you can download the test project here.

I’m sure I’m just missing something simple here. Any help would be greatly appreciated.

A: 

When showing your ModalViewController in TestViewController displayModal:, you release your modalController (line 20). Don't do this - you need the ViewController to stay alive. If you release it, only the view keeps alive (as it is retained when added as a subview).

Also, in ModalViewController hideModalEnded you release modalView, which you didn't retain, so I'd remove that one as well.

So now you need to release just the instance of ModalViewController after the view got removed. You can do this by [self release]; in hideModalEnded, but this seems to be an unusual pattern and I don't feel good doing it.

Some suggestions:

  • Keep the show and hide methods in the same class.
  • Keep an ivar around with the controller.
  • Another possiblity: Remove the ModalViewController altogether and put everything in TestViewController - But this very much depends on how much action there will be going on in the real thing.
Eiko
Ha! That was simple. Thank you for saving me hours of debug!
Cuzog
Same test app, new bug. Not sure if this is related to the release, but if you open the modal and dismiss it again 7 times the 8th time will crash the app. The debugger shows "EXC_BAD_ACCESS."
Cuzog
Sorry, mixed up view and controller and the release slipped through... I have no clue why it fails consistently on the 8th incarnation though.
Eiko
Thanks for the followup! [self release] fixes it. I assume the view wasn't getting cleared from memory and after enough of the views piled up, it crashed. In my real app, the view crashed after 3 times because the modal contains a lot more elements.You mention a better way to do it would be to put the show and hide methods in the same class. I would like to do this, but don't know how to hook up the hide method to the close button when it is present in a separate xib file. That modal view is quite complex in the real app, so it needs its own controller and xib.Thanks again!
Cuzog