set selectedRange in a UITextView
A short Cocoa Touch post as I could not immediately find the solution on the internets.
I wanted the cursor to be at the beginning of a UITextView when the user taps anywhere in that text view. The straightforward approach of doing it in the delegate does not work and I found a couple of posts saying so.
It turns out that performSelector:withObjects:afterDelay is your friend:
- (void)textViewDidBeginEditing:(UITextView *)inView
{
[self performSelector:@selector(setCursorToBeginning:) withObject:inView afterDelay:0.01];
}
- (void)setCursorToBeginning:(UITextView *)inView
{
inView.selectedRange = NSMakeRange(0, 0);
}