Sometimes it’s necessary to perform an action some time in the future, whether it’s disabling a button for a certain time interval after it’s pressed, performing an animation after a short wait, or triggering a reload of some data.
NSTimer is great for that purpose, as well as repeatedly performing actions, but it’s most convenient utility method, scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
only takes one ‘id
‘ argument. Using the NSInvocation equivalent, scheduledTimerWithTimeInterval:invocation:repeats:
requires creating and setting up the NSInvocation itself, which is always verbose and a pain.
NSProxy is a wonderful little construct that lets us interact with it like we were talking to the original object. I first learned how it works from a post by Shaun Wexler which shows an easier way to send a message on the main thread, by using the NSInvocation given via the NSProxy’s forwardInvocation:
method. The same technique can be used to easily create a configured NSTimer.
So, instead of some awful thing like this:
NSInvocation *i = [NSInvocation invocationWithMethodSignature:[mapView methodSignatureForSelector:@selector(selectAnnotation:animated:)]];
[i setTarget:mapView];
MKAnnotation *annotation = view.annotation;
[i setArgument:&annotation atIndex:3];
BOOL flag=YES;
[i setArgument:&flag atIndex:4];
[NSTimer scheduledTimerWithTimeInterval:0.5 invocation:i repeats:NO]; |
NSInvocation *i = [NSInvocation invocationWithMethodSignature:[mapView methodSignatureForSelector:@selector(selectAnnotation:animated:)]];
[i setTarget:mapView];
MKAnnotation *annotation = view.annotation;
[i setArgument:&annotation atIndex:3];
BOOL flag=YES;
[i setArgument:&flag atIndex:4];
[NSTimer scheduledTimerWithTimeInterval:0.5 invocation:i repeats:NO];
We can do this:
[(MKMapView*)[TPTimerProxy scheduledTimerProxyWithTarget:mapView timeInterval:0.5 repeats:NO] selectAnnotation:view.annotation animated:YES]; |
[(MKMapView*)[TPTimerProxy scheduledTimerProxyWithTarget:mapView timeInterval:0.5 repeats:NO] selectAnnotation:view.annotation animated:YES];
Here’s the juice: Read More »
A drop-in universal solution for moving text fields out of the way of the keyboard
There are a hundred and one proposed solutions out there for how to move
UITextField
andUITextView
out of the way of the keyboard during editing — usually, it comes down to observingUIKeyboardWillShowNotification
andUIKeyboardWillHideNotification
, or implementingUITextFieldDelegate
delegate methods, and adjusting the frame of the superview, or usingUITableView
‘sscrollToRowAtIndexPath:atScrollPosition:animated:
, but all the proposed solutions I’ve found tend to be quite DIY, and have to be implemented for each view controller that needs it.I thought I’d put together a relatively universal, drop-in solution:
UIScrollView
andUITableView
subclasses that handle everything.When the keyboard is about to appear, the subclass will find the subview that’s about to be edited, and adjust its frame and content offset to make sure that view is visible, with an animation to match the keyboard pop-up. When the keyboard disappears, it restores its prior size.
It should work with basically any setup, either a UITableView-based interface, or one consisting of views placed manually.
Read More »