b2cloud

12th December 2012

UITextField Monitoring selectedRange

Guides | Tutorial By 4 years ago

In the rare case that you need to monitor UITextField selection changes, there are very few ways to do this. The first is with method swizzling, however this involves messing with private APIs, which can be dangerous. The second is in a place you wouldn’t expect to find it, which is what I’m going to cover.

This example will use my UITextField Selection category from last week, so check that out if you haven’t already.

First of all you’ll need to subclass UITextField. Add a delegate property, I will call mine “selectionDelegate” so it doesn’t interfere with UITextField‘s own delegate property.

@protocol SelectionTextFieldDelegate 
@required
- (void) selectionTextField:(SelectionTextField*) sender changedSelectedRange:(NSRange) selectedRange;
@end

@property (nonatomic, assign) IBOutlet id selectionDelegate;

@synthesize selectionDelegate;

Next I will make a NSRange property for holding our previous selection range. The method we will override will get called multiple times, so this will prevent unnecessarily getting called when we don’t want to know about it.

@interface SelectionTextField ()
{
	NSRange oldSelectedRange;
}

@end

...

// In an init method somewhere...
oldSelectedRange = NSMakeRange(NSNotFound, NSNotFound);

Now time for the magic. Override this method and implement the following:

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
	const BOOL result = [super canPerformAction:action withSender:sender];

	const NSRange selectedRange = self.selectedRange;

	if(!NSEqualRanges(selectedRange, oldSelectedRange))
	{
		oldSelectedRange = selectedRange;
		[selectionDelegate selectionTextField:self changedSelectedRange:selectedRange];
	}

	return result;
}

And if you’re super lazy, just download the source here.

Recommended Posts

Retain Loops and Delegates

Post by 4 years ago

When using delegates in an object in Objective-C it is important that the delegate is only assigned within your object, and never retained. The reason for this is to prevent a retain loop, where two...

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!