Nifty trick: the definitive way of implementing tab index to text fields
In the previous article, I told you how to add a property to a category – a task that's usually not easily done. We're going to use that knowledge now to implement a proper tab index and when I say proper, I mean without the use of tags, manually setting the firstResponder (because that's about as subtle as a gynecologist wearing a gas mask) or any subclassing nonsense.
I know what you're probably thinking right now: what do I even mean by “tab index”? Has [objectively:smart] suddenly turned into a blog about guitars? Well, fear not, a trivial explanation (that you can safely skip if you've ever created a simple HTML form) says that the tab index defines the order in which various user interface elements within a given form get activated whenever user presses the Next button on the iOS keyboard. Now that I think about it, a more fitting name would be something along the lines of “next index”, but that sounds… stupid. The term “tab index” itself comes probably from the ancient times when the birthday cake of HTML forms didn't look like a brush fire yet and a tabindex attribute of a HTML tag used to define which page element would get active whenever user pressed the Tab key.
Well, enough of the history lesson, let's do some programming!
First things first, you're going to need the DProperty library1 and the UITextField extension from previous blog post, so if you haven't done so already, now would be the perfect time to read it and implement these two things. When you're finished doing that, let's enhance the view controller containing all the sneaky little text fieldses in two simple ways: first, you want the view controller to conform to the UITextFieldDelegate protocol, so you edit the header file like this:
Open your storyboard file (or xib file, grandpa) in Interface Builder and set the view controller you just edited as the delegate of all instances of UITextField that are going to have a tab index by holding down the right mouse button and dragging, just like this:
Here comes the last step, this time drag a line from each UITextField that is going to be part of the sequence to the next UITextField in line and define natural and logical order of the text fields:
And… that's about it, actually! The most elegant solution to the problem that many iOS developers face!
Technically, you don't exactly need the DProperty library if you're willing to create the accessor methods by yourself – you'd just use the
id objc_getAssociatedObject(id object, void *key) and
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy) functions. ↩︎
Sparco Leather Monthly Tab Index
Index dividers feature black leather tabs with gold imprints. Monthly tab indexes range from January to December. Heavy-duty buff ledger paper has a gold-reinforced binding edge for extra strength. Dividers are punched for standard three-ring binders.Packaged Quantity: 1 SetProduct Type: Index DividerDividers: Letter - 8.50" Width x 11" LengthTabs: LeatherTab Color: BlackTitle: PrintedHoles: 3 x PunchedFeatures: PunchedReinforced - Gold Binding EdgeHeavy DutyRecycled: YesRecycled Content: 30%Post-consumer-waste%: 30%Assembly Required: No Get th¡s 0ne!с. Dοnοt míss thе сhαncё!