UIKit text, forms, keyboards, focus, and validation
UIKit form controls edit temporary input. The persisted note changes only after the draft passes validation and Save succeeds.
Keep a draft beside the controls
struct NoteDraft: Equatable { var title: String var body: String
var titleError: String? { title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? "Enter a title." : nil }}Text-field and text-view delegates copy edits into this draft. Cancel compares it with the original. A save failure preserves it.
Configure input as a hint
titleField.placeholder = "Title"titleField.returnKeyType = .nexttitleField.textContentType = niltitleField.adjustsFontForContentSizeCategory = trueKeyboard type, capitalization, and content type help entry. They do not validate pasted text, dictation, hardware keyboards, or automation.
Move focus intentionally
func textFieldShouldReturn(_ textField: UITextField) -> Bool { bodyView.becomeFirstResponder() return true}Validation failure can focus the first invalid control and expose a nearby error. VoiceOver announcements and reading order require separate testing.
Follow the keyboard layout guide
Constrain the form or bottom action region against keyboardLayoutGuide when the deployment floor supports it. Do not translate the whole root view by notification height. Split keyboards, floating keyboards, rotation, hardware keyboards, and interactive dismissal break that assumption.
Validate one action path
Toolbar Save, keyboard shortcut, and Return submission call the same method. The method updates attempted-save state, renders errors, builds a domain command, prevents duplicate work, and preserves the draft on failure.
Validation boundary
Text entry, delegate order, focus, keyboard geometry, validation announcements, hardware input, and Dynamic Type remain Not verified.
Series navigation
- Previous: Part 51: UIKit responder chain, gestures, menus, and input
- Next: Part 53: UIKit table views, reuse, prefetching, and diffable data
- Series index: Zero to iOS Hero
References
- UITextField provides single-line text input.
- UITextView provides multiline editing.
- UIKeyboardLayoutGuide represents keyboard-obscured layout.