xcode 在调用 RespondsToSelector 之前检查委托是否仍然存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5091693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:17:59  来源:igfitidea点击:

Check if delegate still exists before calling respondsToSelector

iphoneobjective-cxcodeios

提问by user609906

I have made a UIViewsub 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 delegateto nilin my view controller's deallocimplementation.

我制作了一个UIView子类来处理 iAds 和 adMob。视图控制器是子类委托,一切正常。然而,有时视图控制器在广告被获取之前就消失了。要解决此我设置delegatenil在我的视图控制器的dealloc实现。

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?

respondsToSelector:遇到的问题是,有时该方法在视图控制器被释放的同时运行。这会导致崩溃。有谁知道我如何重新安排这个?

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

广告视图在大约 10 个不同的视图控制器上,所以我想要一个地方来创建广告代码。

Many Thanks

非常感谢

回答by futureelite7

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.

一种简单的方法是保留视图,直到您有机会将委托设置为 nil,然后释放它。然后您可以确保该对象仍然存在并防止崩溃。

回答by Matt Connolly

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

如果无法保留视图,则使用静态方法获取在 dealloc 中清除的实例。即:而不是:

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;
}

回答by BabyPanda

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

虽然这是很久以前的问题,但我真的把它弄乱了一点,终于找到了可能有帮助的东西。

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

为完成或失败的事件设置完成块而不是委托,这会有所帮助。

回答by Marcos Crispino

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

回答by Stan

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/@catch 块。就当你到达 @catch 块时,你的委托肯定会失败......比如:

@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
}

回答by Rahul Vyas

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.

是的,这是 iAd 和 admob 的问题。我也遇到过这种问题。我已经通过在主窗口上添加添加视图解决了这个问题,并使委托给应用程序委托,这样应用程序委托在您关闭应用程序之前永远不会被释放。

回答by Cocoanetics

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.

你不应该有 10 次单独的广告浏览,这在很多层面上都是错误的。取而代之的是,您应该只有一个可以在各个视图之间移动,或者 - 更聪明的 - 保持在顶部。

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.

例如,您可以向 tabBarController.view 添加一个视图,即使您切换选项卡,它也会保持存在。对于不想显示广告的视图,您可以简单地将其隐藏。