I have a scroll view holding a few images, each one is around 100KB. I add them to uiscrollview by creating uiviewcontrollers to hold them. controllers are stored in the scrollview. I remove its view from superview and replace it by some string when the image scrolls out of visible area. I think I’m doing fine. But I still got crash after scrolling a few times(even scroll forth and back on the same 5 images).
I noticed every controller’s dealloc is called when it’s removed from the scroll view, but not its viewDidUnload.
Any help is appreciated.
viewDidUnloadis called when theviewControlleritself released its view. It is not called if you released the view (which meansviewDidUnloadis only relevant in the memory warning situation). In order to completely delete a view and its view controller, you should remove the view from its superview, then release the view controller. Then it’s normalviewDidUnloadis not called.It’s hard to understand how you implemented this, and why it went wrong. But let me suggest a few things.
The view controller’s methods such as
viewWillAppear,viewDidAppear,viewWillDisappear,viewDidDisappear, etc. are not useful here, because none of them can tell you the view is currently on the screen or not. The only way you can tell is to get thecontentOffsetof the scroll view every time a scroll happens (by delegate methods of UIScrollViewDelegate), and check yourself if those views are on the visible region or not.Therefore, it’s not necessary to use UIViewController there. Just use views, for example UIImageView. Dynamically create an instance of the view when it’s visible, or more desirably, when it’s near the visible area.