Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if delegate still exists before calling respondsToSelector

I have made a UIView sub class to deal with iAds and adMob. The view controller is the sub class delegate and everything works ok. Sometimes however, the view controller disappears before the ad has been fetched. To resolve this I set the delegate to nil in my view controller's dealloc implementation.

The problem I have is that sometimes the respondsToSelector: method is ran at the same time as the view controller is getting deallocated. This causes a crash. Does anyone know how I may rearrange this?

The ad view is on about 10 different view controllers so I would like one place to create the ad code.

Many Thanks

like image 954
user609906 Avatar asked Feb 23 '11 13:02

user609906


1 Answers

One easy way is retaining the view until you had a chance to set the delegate to nil, and then release it. Then you can ensure that the object is still alive and prevent crashes.

like image 103
futureelite7 Avatar answered Sep 22 '22 18:09

futureelite7