how to call a method of multiple arguments with delay

后端 未结 6 572
谎友^
谎友^ 2020-12-13 04:15

I\'m trying to call a method after some delay.

I know there is a solution for that:

[self performSelector:@selector(myMethod) withObject:nil afterDel         


        
6条回答
  •  北海茫月
    2020-12-13 04:36

    Other ideas:

    1) You could use NSInvocations:

    + (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)signature
    (>> see Eldar Markov's answer)

    Documentation:
    https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html

    2) You could use a helper method..

    [self performSelector:@selector(helperMethod) withObject:nil afterDelay:delay];
    
    - (void) helperMethod
    {
        // of course x1 and x2 have to be safed somewhere else
        [object moveSomethigFrom: x1 to: x2];
    }
    

    3) You could use an array or a dictionary as parameter..

    NSArray* array = [NSArray arrayWithObjects: x1, x2, nil];
    [self performSelector:@selector(handleArray:) withObject:array afterDelay:delay];
    
    - (void) handleArray: (NSArray*) array
    {
        [object moveSomethigFrom: [array objectAtIndex: 0] to: [array objectAtIndex: 1]];
    }
    

提交回复
热议问题