Saturday, May 11, 2013

NSInvocation setSelector example ios


setSelector:

Sets the receiver’s selector.
- (void)setSelector:(SEL)selector
Parameters
selector
The selector to assign to the receiver.


( NSInvocation setSelector example )

NSMethodSignature *sig = nil;
sig = [[self class] instanceMethodSignatureForSelector:@selector(viewDidAppear:)];
NSInvocation *myInvocation = nil;
myInvocation = [NSInvocation invocationWithMethodSignature:sig];

[myInvocation setTarget:_somePopoverController];
[myInvocation setSelector:@selector(dismissPopoverAnimated:)];
BOOL animate = YES;
[myInvocation setArgument:&animate atIndex:2];
[myInvocation invoke];

( NSInvocation setSelector example )
NSString * parameterSignature = @"v@:";
NSMethodSignature * signature = [NSMethodSignature signatureWithObjCTypes:[parameterSignature UTF8String]]; 
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(aMethodWithNoParms)];
( NSInvocation setSelector example )
- (void)hello:(NSString *)hello world:(NSString *)world
{
    NSLog(@"%@ %@!", hello, world);

    NSMethodSignature *signature  = [self methodSignatureForSelector:_cmd];
    NSInvocation      *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setTarget:self];                    // index 0 (hidden)
    [invocation setSelector:_cmd];                  // index 1 (hidden)
    [invocation setArgument:&hello atIndex:2];      // index 2
    [invocation setArgument:&world atIndex:3];      // index 3

    // NSTimer's always retain invocation arguments due to their firing delay. Release will occur when the timer invalidates itself.
    [NSTimer scheduledTimerWithTimeInterval:1 invocation:invocation repeats:NO];
}