0

Is there a built-in way (rather than using my own flags) to differentiate when viewDidAppear is called because the view controller was pushed to the navigation stack, rather than when a modal disappears?

In other words, I need to execute code when the view appears only when it is pushed to the navigation stack. When I perform the action I'm presenting a view controller and dismissing it automatically. Upon dismissing it, the viewDidAppear gets called again, entering in an infinite loop.

How can I avoid this infinite loop?

This question is related to a modal viewControllerand not regarding coming from and to the viewController through navigation.

Adi
  • 5,089
  • 6
  • 33
  • 47
Jonas Stawski
  • 6,682
  • 6
  • 61
  • 106
  • 2
    Check if `viewDidLoad` is called when you dismiss the view controller. – danypata Jun 26 '13 at 16:14
  • @danypata Since `viewDidLoad` is only called once and it will be called before the first ever call to `viewDidAppear`, checking for `viewDidLoad` is not a solution. – rmaddy Jun 26 '13 at 16:18
  • @A-Live Edited to say view controller – Jonas Stawski Jun 26 '13 at 16:26
  • @danypata viewDidLoad is not being called – Jonas Stawski Jun 26 '13 at 16:27
  • why don't you use navigationController's delegate method to execute your code that you're currently running in viewDidAppear? This ensures the code to be run only when the navigation stack is pushed. – John Jun 26 '13 at 16:30
  • Did anyone read the accepted answer to the duplicate question? Use the `isBeingPresented` method. – rmaddy Jun 26 '13 at 16:41
  • @maddy `isBeingPresented` is `false` on both occasions – Jonas Stawski Jun 26 '13 at 16:48
  • @JonasStawski Are you calling it from `viewDidAppear:`? Are you also calling `[super viewDidAppear:animated]` in your implementation? – rmaddy Jun 26 '13 at 17:03
  • @JonasStawski Is this on an iPhone/iPod touch or an iPad? – rmaddy Jun 26 '13 at 17:07
  • @rmaddy iPad. I am calling it from `viewDidAppear` and also calling `[super viewDidAppear:animated]` – Jonas Stawski Jun 26 '13 at 18:02
  • If the `viewDidLoad` is not called when you dismiss the view controller, then that's your solution. `viewDidLoad` will be called when the view controller is pushed, so put a flag there and check it in `viewWillAppear`. – danypata Jun 27 '13 at 07:15
  • @danypata the viewDidLoad won't be called when I push the viewcontroller again as I am not instantiating it every time. – Jonas Stawski Jun 27 '13 at 13:57

1 Answers1

0

If you are targeting iOS >= 5.0 use this method in viewDidAppear:

[self isMovingToParentViewController]
lassej
  • 6,256
  • 6
  • 26
  • 34
  • isMovingToParentViewController is true on both cases! – Jonas Stawski Jun 26 '13 at 16:36
  • That shouldn't happen ... – lassej Jun 26 '13 at 17:22
  • Except maybe if you present and dismiss the other view controller in the same RunLoop cycle. Are you doing that? – lassej Jun 26 '13 at 17:23
  • how would I know if it's the same RunLoop? – Jonas Stawski Jun 27 '13 at 13:55
  • If you call present... and dismiss... in the same method chain it's the same runloop cycle. If the dismiss is triggered via timer or via separate click it's a different cycle. – lassej Jun 27 '13 at 14:25
  • If you call present... and dismiss... in the same method chain, the question would be why you do that. – lassej Jun 27 '13 at 14:25
  • this is a Kiosk app using a receipt printer. We print by opening a view, grabbing the bytes off a print screen, and sending them to the printer. All without user interaction. That's why we present and dismiss within the same Method Chain – Jonas Stawski Jun 28 '13 at 01:15
  • ah, ok. isMovingToParentViewController returning YES the second time in this case looks like an SDK bug. You might work around it by sending the opening / closing of the second viewcontroller to the next runloop cycle ([self performSelector: ... withObject: ... afterDelay: 0.0]). – lassej Jun 28 '13 at 05:36