On my iPod Touch (640 × 1136),
A
UITextView
is like the
UITextField
we saw
here,
except that a
UITextView
can hold many lines of text.
It can also scroll up and down because it is derived from class
UIScrollView
.
I put a
ViewController
on top of the
UITextView
,
and a
UITabBarController
on top of the
ViewController
,
because everyone expects the
UITextView
to be displayed under a navigation bar containing an
Edit/Done button.
The
ViewController
contains the
edit
method called when the user presses the Edit button,
which calls
becomeFirstResponder
to display the keyboard.
The
ViewController
also acts as the
UITextView
’s
delegate,
containing the methods that are called when the user finishes editing.
These methods are
textViewShouldEndEditing(_:)
and
textViewDidEndEditing(_:)
,
and are triggered by the Edit/Done button.
The Supporting Files
folder contains the text file
undedited.txt
.
When the app is first launched, the
loadView
method of the
ViewController
displays this file in the
UITextView
.
Each time editing ends, the
textViewDidEndEditing(_:)
method of the
ViewController
writes the edited text into a file named
edited.txt
.
To confirm that the write to the file was successful, the
ViewController
modally displays an orange view with some statistics.
I put a plain vanilla
UIViewController
on top of the orange view,
and a
UINavigationController
on top of the plain vanilla
UIViewController
,
because everyone expects the orange view to be displayed
under a navigation bar containing an OK button.
To restore the file to its original state, uninstall and re-install the app. On Top of Old Smoky: Wikipedia article, Weavers’ recording.
AppDelegate
pushes the
ViewController
onto the
UINavigationController
.
ViewController
has a
navigationBar
that contains a button that alternates between Edit and Done.
If the keyboard does not appear when you press the Edit button
when running on the iOS Simulator,
Hardware →
Keyboard →
Toggle Software Keyboard
If you’re running on the iOS Simulator,
you can search for
edited.txt
Terminal.app
find / -name edited.txt 2> /dev/null /Users/mark/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Data/Application/A9A9FAD9-960A-444F-B970-52DE97195D54/Documents/edited.txt cat /Users/mark/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Data/Application/A9A9FAD9-960A-444F-B970-52DE97195D54/Documents/edited.txt
The
textViewDidEndEditing(_:)
.
method of class
ViewController
writes the
text
of the
UITextView
into a file named
exited.txt
.
I had to use
NSUTF8StringEncoding
because
NSASCIIStringEncoding
gave me the error message
“The operation couldn’t be completed. (Cocoa error 517.)”
when the text contained
emoji
characters such as this
\u1F601
😁
func textViewShouldEndEditing(textView: UITextView) -> Bool { if textView.text == "" { return false; //Keep the keyboard visible. } else { return true; //Hide the keyboard and call textViewDidEndEditing(_:). } }which can be abbreviated to
func textViewShouldEndEditing(textView: UITextView) -> Bool { return textView.text != ""; }or to
func textViewShouldEndEditing(textView: UITextView) -> Bool { return !textView.text.isEmpty; }