Because you're calling the [self openGallery];
method from viewDidAppear
, openGallery
will get called every time the view appears.
What is happening is:
SecondPage loads and then appears -> GalleryView Displayed -> Image selected -> Gallery view disappears -> SecondPage appears
When you dismiss the gallery view, viewDidAppear
is called on SecondPage again, which is why you get a never ending loop.
This is part of the viewController lifecycle. Because only one viewController is being presented at a time, even though SecondPage was the view that was presenting the picker, it calls viewDidAppear
again.
So what you need to do is call the openGallery
method when the ViewController is first loaded, not every time it's displayed.
You could try putting the [self openGallery]
call in viewDidLoad
, or else have some logic to determine whether you want the gallery to be displayed in viewDidAppear
or not.
This question also has a good answer to understand the viewController life-cycle: Looking to understand the iOS UIViewController lifecycle