使用performSelector:withObject:afterDelay:使用非对象参数

Bil*_*ill 10 iphone cocoa objective-c

我想setEditing:animated:在一个表视图上调用稍微延迟.通常,我会使用performSelector:withObject:afterDelay:但是

  1. pSwOaD只接受一个参数
  2. 第二个参数setEditing:animated:是一个原始BOOL - 而不是一个对象

在过去,我会在我自己的班级中创建一个虚拟方法setTableAnimated,然后调用,[self performSelector:@selector(setTableAnimated) withObject:nil afterDelay:0.1f但这对我来说感觉很糟糕.

有没有更好的方法呢?

Mar*_*ves 19

为什么不使用dispatch_queue?

  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 …];
  });
Run Code Online (Sandbox Code Playgroud)


Jon*_*an. 16

你需要使用NSInvocation:

请参阅此代码,取自此答案,我稍微更改了一下以符合您的问题:

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];
Run Code Online (Sandbox Code Playgroud)