简体   繁体   中英

Can't modally present the previous View Controller

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

If you want to display page 1 again then you should either dismiss the current view controller (ie 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM