0

I have an app (and can't change the architecture now).

Page 1, presents Page 2 (a Tabnav), with presentModalViewController. Then Page 2 can present Page 1 (via a button) with presentModalViewController also.

Problem is when I re-present Page 1, the App crashes, because page 1 is already presented or something. I can dismiss page 2, which shows page 1, but I can't take that option, because there are other pages that could be presented modally and then they'd be a level below on the stack instead of Page1.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <AccountViewController: 0xc3824a0>.'
*** First throw call stack:
(0x1f3d012 0x1a32e7e 0xa63721 0xa64777 0xa647b7 0x908e 0x1a46705 0x97d920 0xbb9b24 0x1a46705 0x97d920 0x97d8b8 0xa3e671 0xa3ebcf 0xa3dd38 0x9ad33f 0x9ad552 0x98b3aa 0x97ccf8 0x2e48df9 0x2e48ad0 0x1eb2bf5 0x1eb2962 0x1ee3bb6 0x1ee2f44 0x1ee2e1b 0x2e477e3 0x2e47668 0x97a65c 0x258d 0x24b5 0x1)
libc++abi.dylib: terminate called throwing an exception
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
williamsandonz
  • 15,864
  • 23
  • 100
  • 186
  • Show us the crash log, **maybe** we can help you then – MCKapur Jan 09 '13 at 23:30
  • 1
    It sounds like you really should change at least a part of the architecture because that sounds like a nightmare. Presenting modal view controllers shouldn't be used in that sense if I am reading your description correctly. – rooster117 Jan 09 '13 at 23:33
  • 1
    The crash is totally normal as you tried to present a view that is still here (in background of Page 2). But i still can't understand why you can't dismiss it .. plz provide more details – Yaman Jan 09 '13 at 23:34
  • I've tried dismissing it and it doesn't work :( I just call [VC dismissModalViewControllerAnimated:YES] ? Right? – williamsandonz Jan 09 '13 at 23:44
  • 1
    If you're developing for iOS 6 you should use the new selectors, `presentViewController:animated:completion:` and `dismissViewControllerAnimated:completion:` – Ephemera Jan 09 '13 at 23:47

3 Answers3

1

If you want to display page 1 again then you should either dismiss the current view controller (i.e. page 2) or you should create a new instance of page 1 and modally present that.

It's a bad idea to start presenting anything though when you're already in a modally presented view controller.

You should present a modal vc and then dismiss it. That should be it.

Re-reading your OP. Seriously, throw away the structure and redesign. what you have seems to be a mess. If you have lots of transitions why not just use a navigation controller?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
1

You won't be able to present Page1 from Page2 as Page1 is already on the modal stack. "Page1 is already presented" is Xcode's shorthand way of saying this.

As you have to continue using the modal stack, what you may be able to do is carefully articulate it so that, at any time, you can get to any page by presenting the necessary ViewController OR, dismissing a number of ViewControllers in order to 'jump' to a specific page on the stack.

wL_'s answer to this question is a good place to start for an explanation of how to dismiss more than one modal ViewController at a time (note the selector names have changed slightly in iOS 6). Of course, if you have LOTS of modal ViewControllers you're going to be spending quite a bit of memory tracking where each page is in the stack.

I should point out that this is rather bad design - but as you said you can't change the architecture, this is one way of getting around the problem.

Pseudocode:

Button pressed to jump to page:
    PageIsInStackAlready?
        Yes
           Calculate/retrieve page position in stack
           Dismiss necessary number of ViewControllers
        No
           Present new modal ViewController
           Store/account for new page's location on stack

As I say, this could work, but it's definitely fighting the system.

Community
  • 1
  • 1
Ephemera
  • 8,672
  • 8
  • 44
  • 84
1

Do you understand the difference between a class and an instance? If you want to present an AccountViewController, you need to make different AccountViewController instance and present that. It sounds like what you're trying to do is present the very same AccountViewController instance that's already in the interface. Obviously that's impossible.

I agree with others that your interface is just bad design, but at least this answers the linguistic question you're asking.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for that, Oh I was under the impression you would just have one instance of each view controller per App cycle, I guess that is completely wrong? – williamsandonz Jan 09 '13 at 23:41
  • There's certainly no such limitation. An instance is an instance; the whole point of a class is that you can instantiate it as many times as necessary. For example, consider a UIPageViewController that displays two pages at once. If we have only one page type, represented by the MyCoolPage view controller class, obviously we need at least two instances of that class at all times. – matt Jan 10 '13 at 04:14