594Delaying Methods. And cancelling the delaying of methods

Delaying a method should be by now fairly clear:

[self performSelector:@selector(myMethod) withObject:nil afterDelay:3];

But what, if you need to cancel the delayed perform request? That's the way to do it:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];

'cancelPreviousPerformRequestsWithTarget' seems to be the current leader in the 'Longest Objective C Method Name Competition" with 39 characters, not bad.

Let me know, if you find any more contenders...

536Adding a custom delegate

MyClassWithDelegate.h

#import 

@protocol MyClassWithDelegaet 
@optional
- (void)myDelegateMethod:(NSString*)tsst;
@end

@interface MyClassWithDelegate : NSView {
    id  delegate;
}

@property (nonatomic, assign) id  delegate;

@end

 

MyClassWithDelegate.m

#import "MyClassWithDelegate.h"

@implementation MainView

@synthesize delegate;

- (void)anyEvent:(NSEvent *)e { // an event or something like that
    // send delegate
    [[self delegate] myDelegateMethod:@"sss"];
}

@end

Thanks to Jon Sterling for his digest of Apple's 'How Delegation Works'

374Delay method execution, part II

[NSTimer scheduledTimerWithTimeInterval:1.0 
target:self selector: @selector(methodName:)  
userInfo:nil repeats:NO];   

Alternative to Part I [35], has the possibility of repetetive calls.

58Delay method execution & passing objects along the way

Specific example. Deselect table cell after a certain time. TableView and indexPath are wrapped into an array and send along to the method for delayed execution.

  • (void)tableView:(UITableView )tView didSelectRowAtIndexPath:(NSIndexPath )indexPath { NSLog(@"User selected row %d\n", [indexPath row] + 1); // actions [self performSelector:@selector(deselectTableCell:) withObject:[NSArray arrayWithObjects:tView, indexPath, nil] afterDelay:0.2f]; }

-(void) deselectTableCell:(NSArray *)array { [[ [array objectAtIndex:0] cellForRowAtIndexPath:[array objectAtIndex:1] ] setSelected:NO]; NSLog(@"deselectTableCell" ); }

In additon to http://codec.trembl.org/35/

35Delay method execution

[self performSelector:@selector(methodName) withObject:nil afterDelay:0.10f];

update: http://codec.trembl.org/58/