Question

This is probably crazy, but I'm wondering whether or not we can write the actual method inside a selector. Reason being, I just have a simple 1-liner for my method.

-(void)doThat {
    loadedPhoto.alpha = 1.0;
}

-(IBAction)revealPickers:(id)sender {

    loadedPhoto.alpha = 0.7;
    [self performSelector:@selector(doThat) withObject:nil afterDelay:1.0];
}

So what I'd like to know is there a shortcut to this like (obviously this won't work):

[self performSelector:@selector(^(void){loadedPhoto.alpha=1.0;}) withObject:nil afterDelay:1.0];

Possible or just stupid?

Was it helpful?

Solution

Expanding on and correcting my comment:

My comment is wrong, I misread your code and thought you were doing [self performSelectorOnMainThread:etc:].

My actual solution (after a quick googling - it wasn't that hard to find):

// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_current_queue(), ^(void){
    loadedPhoto.alpha = 1.0;
});

Hope it helps!

OTHER TIPS

The easist solution is to use dispatch_after but you can also define the methods on NSObject using a block adapter.

@interface BlockAdapter

@property (nonatomic, copy, readwrite) void (^block)();

- (void)call;

@end

@implementation BlockAdapter

- (void)call {
    self.block();
}

@end
@interface NSObject (PerformBlocks)

- (void)performBlock:((void)^())block afterDelay:(NSTimeInterval)delay;

@end

@implementation NSObject (PerformBlocks)

- (void)performBlock:((void)^())block afterDelay:(NSTimeInterval)delay {
   BlockAdapter* adapter = [[BlockAdapter alloc] init];
   adapter.block = block;

   [adapter performSelector:@selector(call) withObject:nil afterDelay:delay];
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top