iOS 6.0中的CADisplayLink不保留目标 [英] CADisplayLink at iOS 6.0 not retaining target
问题描述
我有这样的代码:
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(updateFrame)]];
[invocation setTarget:self];
[invocation setSelector:@selector(updateFrame)];
displayLink_ = [[CADisplayLink displayLinkWithTarget:invocation selector:@selector(invoke)] retain];
[displayLink_ setFrameInterval:1];
[displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
在这个代码调用我有两个变种的iOS 6.0(在5.1中,这段代码可以正常工作):EXC_BAD_ACCESS或'调用无法识别的选择器'调用'。似乎displayLinkWithTarget:selector:方法不保留目标。当我添加[invocation retain]行时,代码变得正常工作。这是iOS 6.0的错误吗?
At iOS 6.0 (in 5.1 this code works ok) when this code calling I have two variants: EXC_BAD_ACCESS or 'call to unrecognized selector "invoke"'. It is seem that displayLinkWithTarget:selector: method doesn't retain target. When i add [invocation retain] line, code become to work ok. Is it bug of iOS 6.0?
推荐答案
这是有用的相关信息,而不是答案。
而不是使用 NSInvokation
你可以使用弱代理,就像我在这个问题的实际答案中所做的那样。这很简单,这是代码:
Rather than use NSInvokation
you can use a weak proxy as I have done in my actual answer to this question. It's very simple, here's the code:
JAWeakProxy.h:
#import <Foundation/Foundation.h>
@interface JAWeakProxy : NSObject
@property (weak, nonatomic) id target;
+ (JAWeakProxy*)weakProxyWithTarget:(id)target;
@end
JAWeakProxy.m:
#import "JAWeakProxy.h"
@implementation JAWeakProxy
+ (JAWeakProxy*)weakProxyWithTarget:(id)target
{
JAWeakProxy* newObj = [self new];
newObj.target = target;
return newObj;
}
- (BOOL)respondsToSelector:(SEL)sel
{
return [_target respondsToSelector:sel] || [super respondsToSelector:sel];
}
- (id)forwardingTargetForSelector:(SEL)sel
{
return _target;
}
@end
注意:这是ARC代码,如果不使用ARC,你需要在 weakProxyWithTarget:
中 autorelease
。
NOTE: This is ARC code, you'll need to autorelease
in weakProxyWithTarget:
if not using ARC.
这篇关于iOS 6.0中的CADisplayLink不保留目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!