开发者

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 proble开发者_如何学编程m 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


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.


If you can't retain the view, then use a static method to get the instance which is cleared in dealloc. ie: instead of:

if (delegate && [delegate respondsToSelector:@selector(...)])

Do this:

if (s_myViewDelegate && [delegate respondsToSelector:@selector(...)])

In the class:

- (id) init {
    s_myViewDelegate = self;
    ...etc...
}

- (void) dealloc {
    s_myViewDelegate = nil;
}


if (delegate && [delegate respondsToSelector:@selector(...)])


Although this is a question long ago, I really messed with it a bit and finally found something that might help.

Set a completion block rather than a delegate for finished or failed event, and this would help.


Yes it's a problem with iAd and admob. I had also this kind of problem. I have solved the problem by adding add view on main window and make delegate to app delegate so app delegate will never deallocated until you close the application.


There is actually a fast and not really good solution - to use @try/@catch block. Just if you get to the @catch block your delegate fails for sure... like:

@try{    
     if (delegate && [delegate respondsToSelector:@selector(...)])
     [delegate callBackMethod];
}
@catch (NSException *e){
     // if you get here then delegate is no longer valid regardless its reference is still valid
}


You should not have 10 individual ad views, that's wrong on so many levels. Instead you should have just one that you either move between individual views or - smarter - just keep on top.

You can for example add a view to a tabBarController.view and this will stay present even if you switch tabs. For views that you don't want the ad on you can simply hide it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜