[APPLE] View Controller Programming Guide for iOS.pdf

(3883 KB) Pobierz
View Controller
Programming Guide for
iOS
Contents
About View Controllers
9
At a Glance
10
A View Controller Manages a Set of Views
10
You Manage Your Content Using Content View Controllers
10
Container View Controllers Manage Other View Controllers
10
Presenting a View Controller Temporarily Brings Its View Onscreen
11
Storyboards Link User Interface Elements into an App Interface
11
How to Use This Document
12
Prerequisites
12
See Also
12
View Controller Basics
14
Screens, Windows, and Views Create Visual Interfaces
15
View Controllers Manage Views
17
A Taxonomy of View Controllers
19
Content View Controllers Display Content
19
Container View Controllers Arrange Content of Other View Controllers
21
A View Controller’s Content Can Be Displayed in Many Ways
26
View Controllers Work Together to Create an App’s Interface
28
Parent-Child Relationships Represent Containment
29
Sibling Relationships Represent Peers Inside a Container
29
Presentation Represents a Transient Display of Another Interface
30
Control Flow Represents Overall Coordination Between Content Controllers
31
Storyboards Help You Design Your User Interface
33
Using View Controllers in Your App
35
Working with View Controllers in Storyboards
36
The Main Storyboard Initializes Your App’s User Interface
37
Segues Automatically Instantiate the Destination View Controller
37
Instantiating a Storyboard’s View Controller Programmatically
39
Containers Automatically Instantiate Their Children
41
Instantiating a Non-Storyboard View Controller
41
Displaying a View Controller’s Contents Programmatically
41
2012-12-13 | Copyright © 2012 Apple Inc. All Rights Reserved.
2
Contents
Creating Custom Content View Controllers
43
Anatomy of a Content View Controller
43
View Controllers Manage Resources
44
View Controllers Manage Views
45
View Controllers Respond to Events
45
View Controllers Coordinate with Other Controllers
45
View Controllers Often Work with Containers
46
View Controllers May Be Presented by Other View Controllers
46
Designing Your Content View Controller
47
Use a Storyboard to Implement Your View Controller
48
Know When Your Controller Is Instantiated
48
Know What Data Your View Controller Shows and Returns
48
Know What Tasks Your Controller Allows the User to Perform
49
Know How Your View Controller Is Displayed Onscreen
50
Know How Your Controller Collaborates with Other Controllers
50
Examples of Common View Controller Designs
50
Example: Game Title Screen
50
Example: Master View Controller
52
Example: Detail View Controller
53
Example: Mail Compose View Controller
54
Implementation Checklist for Custom Content View Controllers
54
Resource Management in View Controllers
56
Initializing a View Controller
56
Initializing a View Controller Loaded from a Storyboard
56
Initializing View Controllers Programmatically
57
A View Controller Instantiates Its View Hierarchy When Its View is Accessed
57
Loading a View Controller’s View from a Storyboard
59
Creating a View Programmatically
60
Managing Memory Efficiently
61
On iOS 6 and Later, a View Controller Unloads Its Own Views When Desired
63
On iOS 5 and Earlier, the System May Unload Views When Memory Is Low
64
Responding to Display-Related Notifications
66
Responding When a View Appears
66
Responding When a View Disappears
67
Determining Why a View’s Appearance Changed
67
Resizing the View Controller’s Views
69
A Window Sets the Frame of Its Root View Controller’s View
69
2012-12-13 | Copyright © 2012 Apple Inc. All Rights Reserved.
3
Contents
A Container Sets the Frames of Its Children’s Views
70
A Presented View Controller Uses a Presentation Context
70
A Popover Controller Sets the Size of the Displayed View
70
How View Controllers Participate in the View Layout Process
70
Using View Controllers in the Responder Chain
72
The Responder Chain Defines How Events Are Propagated to the App
72
Supporting Multiple Interface Orientations
74
Controlling What Interface Orientations Are Supported (iOS 6)
75
Declaring a View Controller’s Supported Interface Orientations
75
Dynamically Controlling Whether Rotation Occurs
76
Declaring a Preferred Presentation Orientation
76
Declaring the App’s Supported Interface Orientations
76
Understanding the Rotation Process (iOS 5 and earlier)
77
Declaring the Supported Interface Orientations
77
Responding to Orientation Changes in a Visible View Controller
78
Rotations May Occur When Your View Controller Is Hidden
80
Creating an Alternate Landscape Interface
80
Tips for Implementing Your Rotation Code
82
Accessibility from the View Controller’s Perspective
83
Moving the VoiceOver Cursor to a Specific Element
83
Responding to Special VoiceOver Gestures
84
Escape
85
Magic Tap
85
Three-Finger Scroll
85
Increment and Decrement
86
Observing Accessibility Notifications
86
Presenting View Controllers from Other View Controllers
88
How View Controllers Present Other View Controllers
88
Presentation Styles for Modal Views
91
Presenting a View Controller and Choosing a Transition Style
93
Presentation Contexts Provide the Area Covered by the Presented View Controller
95
Dismissing a Presented View Controller
95
Presenting Standard System View Controllers
96
Coordinating Efforts Between View Controllers
98
When Coordination Between View Controllers Occurs
98
2012-12-13 | Copyright © 2012 Apple Inc. All Rights Reserved.
4
Contents
With Storyboards, a View Controller is Configured When It Is Instantiated
99
Configuring the Initial View Controller at Launch
100
Configuring the Destination Controller When a Segue is Triggered
101
Using Delegation to Communicate with Other Controllers
103
Guidelines for Managing View Controller Data
105
Enabling Edit Mode in a View Controller
106
Toggling Between Display and Edit Mode
106
Presenting Editing Options to the User
108
Creating Custom Segues
109
The Life Cycle of a Segue
109
Implementing a Custom Segue
109
Creating Custom Container View Controllers
111
Designing Your Container View Controller
111
Examples of Common Container Designs
113
A Navigation Controller Manages a Stack of Child View Controllers
113
A Tab Bar Controller Uses a Collection of Child Controllers
115
A Page Controller Uses a Data Source to Provide New Children
116
Implementing a Custom Container View Controller
116
Adding and Removing a Child
116
Customizing Appearance and Rotation Callback Behavior
119
Practical Suggestions for Building a Container View Controller
120
Document Revision History
122
Glossary
124
2012-12-13 | Copyright © 2012 Apple Inc. All Rights Reserved.
5
Zgłoś jeśli naruszono regulamin