iOS视图控制器编程指南 --- 设计建议

来源:互联网 发布:nvidia mac webdriver 编辑:程序博客网 时间:2024/04/29 04:06

View controllers are an essential tool for apps running on iOS, and the view controller infrastructure of UIKit makes it easy to create sophisticated interfaces without writing a lot of code. When implementing your own view controllers, use the following tips and guidelines to ensure that you are not doing things that might interfere with the natural behavior expected by the system. 

Use System-Supplied View Controllers Whenever Possible

Many iOS frameworks define view controllers that you can use as-is in your apps. Using these system-supplied view controllers saves time for you and ensures a consistent experience for the user.

Most system view controllers are designed for specific tasks. Some view controllers provide access to user data such as contacts. Others might provide access to hardware or provide specially tuned interfaces for managing media. For example, the UIImagePickerController class in UIKit displays a standard interface for capturing pictures and video and for accessing the user’s camera roll. 

Before you create your own custom view controller, look at the existing frameworks to see if a view controller already exists for the task you want to perform.

  • The UIKit framework provides view controllers for displaying alerts, taking pictures and video, and managing files on iCloud. UIKit also defines many standard container view controllers that you can use to organize your content.

  • The GameKit framework provides view controllers for matching players and for managing leaderboards, achievements, and other game features. 

  • The Address Book UI framework provides view controllers for displaying and picking contact information.

  • The MediaPlayer framework provides view controllers for playing and managing video, and for choosing media assets from the user’s library.

  • The EventKit UI framework provides view controllers for displaying and editing the user’s calendar data. 

  • The GLKit framework provides a view controller for managing an OpenGL rendering surface.

  • The Multipeer Connectivity framework provides view controllers for detecting other users and inviting them to connect. 

  • The Message UI framework provides view controllers for composing emails and SMS messages.

  • The PassKit framework provides view controllers for displaying passes and adding them to Passbook.

  • The Social framework provides view controllers for composing messages for Twitter, Facebook, and other social media sites.

  • The AVFoundation framework provides a view controller for displaying media assets. 

IMPORTANT

Never modify the view hierarchy of system-provided view controllers. Each view controller owns its view hierarchy and is responsible for maintaining the integrity of that hierarchy. Making changes might introduce bugs into your code or prevent the owning view controller from operating correctly. In the case of system view controllers, always rely on the publicly available methods and properties of the view controller to make all modifications. 

For information about using a specific view controller, see the reference documentation for the corresponding framework. 

Make Each View Controller an Island

View controllers should always be self-contained objects. No view controller should have knowledge about the internal workings or view hierarchy of another view controller. In cases where two view controllers need to communicate or pass data back and forth, they should always do so using explicitly defined public interfaces.

The delegation design pattern is frequently used to manage communication between view controllers. With delegation, one object defines a protocol for communicating with an associated delegate object, which is any object that conforms to the protocol. The exact type of the delegate object is unimportant. All that matters is that it implements the methods of the protocol. 

Use the Root View Only as a Container for Other Views

Use the root view of your view controller solely as a container for the rest of your content. Using the root view as a container gives all of your views a common parent view, which makes many layout operations simpler. Many Auto Layout constraints require a common parent view to lay out the views properly. 

Know Where Your Data Lives

In the model-view-controller design pattern, a view controller’s role is to facilitate the movement of data between your model objects and your view objects. A view controller might store some data in temporary variables and perform some validation, but its main responsibility is to ensure that its views contain accurate information. Your data objects are responsible for managing the actual data and for ensuring the overall integrity of that data.

An example of the separation of data and interface exists in the relationship between the UIDocument and UIViewController classes. Specifically, no default relationship exists between the two. A UIDocument object coordinates the loading and saving of data, while a UIViewController object coordinates the display of views onscreen. If you create a relationship between the two objects, remember that the view controller should only cache information from the document for efficiency. The actual data still belongs to the document object.

Adapt to Changes

Apps can run on a variety of iOS devices, and view controllers are designed to adapt to different-sized screens on those devices. Rather than use separate view controllers to manage content on different screens, use the built-in adaptivity support to respond to size and size class changes in your view controllers. The notifications sent by UIKit give you the opportunity to make both large-scale and small-scale changes to your user interface without having to change the rest of your view controller code. 

For more information about handling adaptivity changes, see The Adaptive Model.


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米5拔号后黑屏怎么办 手机拔号黑屏怎么办 小米手机打电话黑屏怎么办 小米手机突然黑屏怎么办 小米手机自动黑屏怎么办 手机打电话闪退怎么办 oppor9摔黑屏了怎么办 oppo摔黑屏了怎么办 opp手机来电黑屏怎么办 红米手机开不机怎么办 红米note2死机怎么办 红米note5a黑屏怎么办 红米note4x黑屏怎么办 打电话屏幕不亮怎么办 拨打电话时黑屏怎么办 三星手机打电话黑屏怎么办 三星s7打电话黑屏怎么办 三星手机不能开机怎么办 三星手机开机黑屏怎么办 华为手机打电话黑屏怎么办 金立m5手机壁纸黑屏怎么办 万斯开胶了怎么办 vans鞋鞋底开胶怎么办 手机软件出现闪退怎么办 酷派手机蓝屏怎么办 手机突然间黑屏怎么办 手机闪退怎么办 oppo 手机拨号键盘不见了怎么办 oppor9无限重启怎么办 oppor9手机拨号黑屏怎么办 oppo手机摔黑屏怎么办 vivo手机黑屏打不开怎么办 oppo手机黑屏怎么办啊 oppo手机没反应怎么办 oppo黑屏死机了怎么办 来电闹钟时黑屏怎么办 oppo拨号删除了怎么办 电话拨出后黑屏怎么办 手机老是蹦广告怎么办 手机自动出广告怎么办 电脑没有拨号连接怎么办