@selector - With Multiple Arguments?

前端 未结 9 729
刺人心
刺人心 2020-12-13 02:26

I have been using @selector today for the first time and have not been able to work out how to do the following? How would you write the @selector

相关标签:
9条回答
  • 2020-12-13 02:59

    I had an issue where I needed to use the afterDelay along with multiple arguments to my @selector method. Solution? Use a wrapper function!

    Say this is the function I want to pass to @selector:

    -(void)myFunct:(NSString *)arg1 andArg:(NSString *)arg2 andYetAnotherArg:(NSString *)arg3;
    

    Obviously, I can't even use withObject: withObject: here, so, make a wrapper!

    -(void)myFunctWrapper:(NSArray *)myArgs {
        [self myFunct:[myArgs objectAtIndex:0] andArg:[myArgs objectAtIndex:1] andYetAnotherArg:[myArgs objectAtIndex:2]];
    }
    

    and use it by doing:

    NSArray *argArray = [NSArray arrayWithObjects:string1,string2,string3,nil];
    [self performSelector:@selector(myFunctWrapper:) withObject:argArray afterDelay:1.0];
    

    This way I can have multiple arguments and use the selector with delay.

    0 讨论(0)
  • 2020-12-13 03:00

     

     - (id)performSelector:(SEL)aSelector
               withObject:(id)anObject  
               withObject:(id)anotherObject
    

    From the Documentation:

    This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.

    so in your case you would use:

    [self performSelector:@selector(printText:andMore:)
               withObject:@"Cake"
               withObject:@"More Cake"]
    
    0 讨论(0)
  • 2020-12-13 03:05
    [self performSelector:@selector(printText:andMore) withObject:@"Cake" withObject:@"More Cake"];
    
    0 讨论(0)
  • 2020-12-13 03:06

    As KennyTM pointed out, the selector syntax is

    @selector(printText:andMore:)
    

    You call it with

    performSelector:withObject:withObject. 
    

    ... if you need more arguments or different types, you need to use NSIvocation

    0 讨论(0)
  • 2020-12-13 03:07

    As an alternative for NSInvocation when you have more than two parameters, you can use NSObject's -methodForSelector: as in the following example:

    SEL a_selector = ...
    Type1 obj1 = ...
    Type2 obj2 = ...
    Type3 obj3 = ...
    typedef void (*MethodType)(id, SEL, Type1, Type2, Type3);
    MethodType methodToCall;
    methodToCall = (MethodType)[target methodForSelector:a_selector];
    methodToCall(target, a_selector, obj1, obj_of_type2, obj_of_type3);
    
    0 讨论(0)
  • 2020-12-13 03:07

    Elaborating on Ben-Uri's answer, which can be written way shorter.

    E.g. calling the UIView method - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view can be done as follows:

    SEL selector = @selector(covertPoint:toView:);
    IMP method = [viewA methodForSelector:selector];
    CGPoint pointInB = method(viewA, selector, pointInA, viewB);
    
    0 讨论(0)
提交回复
热议问题