Iphone 使用performSelector:withObject:afterDelay:和非对象参数
我想在表视图上稍微延迟调用Iphone 使用performSelector:withObject:afterDelay:和非对象参数,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,我想在表视图上稍微延迟调用setEditing:animated:。通常,我会使用performSelector:withObject:afterDelay:但是 pSwOaD只接受一个参数 setEditing:animated:的第二个参数是一个基本布尔值,而不是一个对象 在过去,我会在自己的类中创建一个虚拟方法,比如setTableAnimated,然后调用[self-performSelector:@selector(setTableAnimated)with Object:nil af
setEditing:animated:
。通常,我会使用performSelector:withObject:afterDelay:
但是
setEditing:animated:
的第二个参数是一个基本布尔值,而不是一个对象setTableAnimated
,然后调用[self-performSelector:@selector(setTableAnimated)with Object:nil afterDelay:0.1f
,但这让我觉得很尴尬
有更好的方法吗?选择器
设置编辑:动画:
与性能选择器:withObject:afterDelay
不兼容。只能使用0或1个参数和参数(如果有)调用方法必须是对象。因此解决方法就是这样。您可以将布尔值包装在NSValue
对象中,并将其传递给setTableAnimated
方法。您需要使用:
请看这段代码,摘自这段代码,我对其进行了轻微修改,以符合您的问题:
BOOL yes = YES;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self.tableView methodSignatureForSelector:@selector(setEditing:Animated:)]];
[inv setSelector:@selector(setEditing:Animated:)];
[inv setTarget:self.tableView];
[inv setArgument:&yes atIndex:2]; //this is the editing BOOL (0 and 1 are explained in the link above)
[inv setArgument:&yes atIndex:3]; //this is the animated BOOL
[inv performSelector:@selector(invoke) withObject:nil afterDelay:0.1f];
如果你能控制住它,使用NSInvocation抓取器创建一个调用对象&用一行而不是多行延迟调用它:为什么不使用调度队列
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[tableView setEditing …];
});
如果它涉及到创建类,几乎没有一行。我不知道为什么这被否决,对我来说似乎是一个好的解决方案。如果你使用iOS4,这就是这样做的方法。我认为使用变量名,如
BOOL\u yes=yes
会更清楚一些,因为它减轻了一点关于你是否有打字错误(yes vs yes)的困惑。还想指出,使用变量表示YES
而不是直接传递bool的原因:NSInvocation
引发异常:[NSInvocation setArgument:atIndex:::::NULL address argument
。