-
Now you're ready to write
your own delegates.
-
The challenge app for
-
this lesson has three text
fields with customized behavior.
-
A zip code text field that only allows
digits with a maximum of five digits.
-
A dollar sign text field that begins
with the text for zero cents.
-
And then fills in the dollar
figure as digits are added.
-
And finally, a text field that can only
be edited when this switch is set to on.
-
Here in the first field, once I get
to five digits, I can't type anymore.
-
Here in the second field,
the digits fill in from right to left.
-
And there are always two
digits beyond the decimal.
-
By the way, don't forget that
Cmd+k toggles the keyboard.
-
In the third field, the user has to
flip this switch in order to add text.
-
You can see that tapping
inside the first two fields
-
brings up the number pad
instead of the qwerty keyboard.
-
You can set this in the attribute
inspector in storyboard.
-
Actually, let's open up
the utilities pane and
-
take a minute to explore
the attribute inspector.
-
This is a super convenient way to
configure UIkit view components.
-
Here you can set your placeholder text.
-
So you might want to put something like,
type your zip code here.
-
Here you can configure the clear button
like the one you saw in the Safari
-
search bar.
-
Here you can set capitalization.
-
If you add something
like a confirmation code,
-
you might want all characters
to be capitalized.
-
And here's the keyboard type, which for
the zip code we want set to number pad.
-
And just two more tips before
you move on to the challenge.
-
You're going to need to set
the delegates for your text fields.
-
If you want to do this in Storyboard
remember your delegate is
-
just a property.
-
So you're going to look for it in
Storyboard where you always look for
-
properties in the pop-up
connections menu.
-
And here under outlets you
can see your delegate.
-
Also for manipulating text,
you might want to revisit these
-
few lines of code from
the emoji text field delegate.
-
The NSString class has the method named
string by replacing characters in range.
-
It's perfect to use in
a UI text field delegate.
-
This method is helpful when you need
to build new strings from typed input.
-
In order to use this method, SWF strings
are usually used to cast to NSStrings.
-
In this case,
-
the variable newText represents
the text that will be displayed.