0302--iOS之阅读View Controller Programming Guide for iOS---(二)View Controller Definition

 

(一)Defining Your Subclass                                                            --定义你的子类vc

 

You use custom subclasses of UIViewController to present your app’s content. Most custom view controllers are content view controllers—that is, they own all of their views and are responsible for the data in those views. By contrast, a container view controller does not own all of its views; some of its views are managed by other view controllers. Most of the steps for defining content and container view controllers are the same and are discussed in the sections that follow.

            --一般来说你都是通过子类化vc来展示你的app的内容的,所以你使用最多的也都是内容vc,内容vc负责管理它自己的views和数据。然而,容器vc是不曾拥有自己的views的。一些vc是通过其他vc来管理它自己的views的。定义内容或容器vc的大多数步骤是相同的,将在下一节中讨论。

For content view controllers, the most common parent classes are as follows:

       --常见的内容vc父类:

          --列表vc

          --集合vc

          --vc

For container view controllers, the parent class depends on whether you are modifying an existing container class or creating your own. For existing containers, choose whichever view controller class you want to modify. For new container view controllers, you usually subclass UIViewController. For additional information about creating a container view controller, see Implementing a Container View Controller.

          --对于容器vc,你可以选择系统现有的,也可以通过子类化vc来自定义一个。

 

Defining Your UI                                                                                      --定义你的用户界面

 

Define the UI for your view controller visually using storyboard files in Xcode. Although you can also create your UI programmatically, storyboards are an excellent way to visualize your view controller’s content and customize your view hierarchy (as needed) for different environments. Building your UI visually lets you make changes quickly and lets you see the results without having to build and run your app.

            --推荐使用IB搭建vc的UI,形象生动效率高

Figure 4-1 shows an example of a storyboard. Each of the rectangular areas represents a view controller and its associated views. The arrows between view controllers are the view controller relationships and segues. Relationships connect a container view controller to its child view controllers. Segues let you navigate between view controllers in your interface.

            --下图展示了storyboard搭建vc的实例。其中的每一个矩形代表了一个vc和它的views们。图中的两个vc之间的箭头表示一个segue,segue用于vc之间的导航。

Figure 4-1 A storyboard holds a set of view controllers and views

              --包含了一系列vc和views的storyboard

image: ../Art/storyboard_bird_sightings_2x.png

Each new project has a main storyboard that typically contains one or more view controllers already. You can add new view controllers to your storyboard by dragging them from the library to your canvas. New view controllers do not have an associated class initially, so you must assign one using the Identity inspector.

            --每一个项目都包含了一个主故事板,这个主故事板通常包含若干个已存在的vc。你可以通过从库里面拖曳一个vc到画布中,这样就相当于在storyboard里添加vc了。此时故事板中的vc还没有和代码中的vc类绑定,你需要通过xcode的标识检查器(一个xcode的窗口界面)进行相关配置才可以。

Use the storyboard editor to do the following:

             --故事板可以执行以下的操作:

  • Add, arrange, and configure the views for a view controller.

          --添加、排列、配置vc中的views

           --使用outlet关键字连接vc的方法(属性)的代码。

  • Create relationships and segues between your view controllers; see Using Segues.

              --使用segues链接两个vc之间的关系

              --根据尺寸大小类自定义views及其布局

  • Add gesture recognizers to handle user interactions with views; see Event Handling Guide for iOS.

             --添加手势识别器来处理views的互动

If you are new to using storyboards to build your interface, you can find step-by-step instructions for creating a storyboard-based interface in Start Developing iOS Apps Today (Retired).

            --对于stroyboard的新手,建议看超链接的教程。

 

 

Handling User Interactions                                                                                   --处理用户交互

 

An app’s responder objects process incoming events and take appropriate actions. Although view controllers are responder objects, they rarely process touch events directly. Instead, view controllers typically handle events in the following ways.

          --虽然vc是响应者对象,当时它一般不直接处理事件,而是用以下的方式处理事件:

  • View controllers define action methods for handling higher-level events. Action methods respond to:

    • --VC一般用来定义处理高级事件的动作方法,而这些动作方法一般作以下用途:

    • Specific actions. Controls and some views call an action method to report specific interactions.

    •        --响应具体的动作。控件和views通过调用动作方法来报告具体的交互动作。

    • Gesture recognizers. Gesture recognizers call an action method to report the current status of a gesture. Use your view controller to process status changes or respond to the completed gesture.

    •        --响应手势识别器。手势识别器调用一个方法来报告当前手势的状态。使用vc来处理手势状态的改变,或响应手势的完成状态

  • View controllers observe notifications sent by the system or other objects. Notifications report changes and are a way for the view controller to update its state.

          --VC可以订阅系统或者其他对象发出来的通知。通知是一种报告vc状态改变的方式,也是vc更新状态改变的一种方式。

  • View controllers act as a data source or delegate for another object. View controllers are often used to manage the data for tables, and collection views. You can also use them as a delegate for an object such as a CLLocationManager object, which sends updated location values to its delegate.

        --vc可以充当一个数据源,也可以充当其他对象的委托。vc经常用来管理列表、集合视图的数据。

Responding to events often involves updating the content of views, which requires having references to those views. Your view controller is a good place to define outlets for any views that you need to modify later. Declare your outlets as properties using the syntax shown in Listing 4-1. The custom class in the listing defines two outlets (designated by the IBOutlet keyword) and a single action method (designated by the IBAction return type). The outlets store references to a button and a text field in the storyboard, while the action method responds to taps in the button.

        --在下面的代码中,outlet关键字在storyboard中存储了一个按钮和文本框,而动作方法(IBAction)则用于响应按钮上的点击事件

 

Listing 4-1 Defining outlets and actions in a view controller class

         --在vc代码中定义outlet和动作方法。

  1. @interface MyViewController : UIViewController
  2. @property (weak, nonatomic) IBOutlet UIButton *myButton;
  3. @property (weak, nonatomic) IBOutlet UITextField *myTextField;
  4.  
  5. - (IBAction)myButtonAction:(id)sender;
  6.  
  7. @end
  1. class MyViewController: UIViewController {
  2. @IBOutlet weak var myButton : UIButton!
  3. @IBOutlet weak var myTextField : UITextField!
  4.  
  5. @IBAction func myButtonAction(sender: id)
  6. }

In your storyboard, remember to connect your view controller’s outlets and actions to the corresponding views. Connecting outlets and actions in your storyboard file ensures that they are configured when the views are loaded. For information about how to create outlet and action connections in Interface Builder, see Interface Builder Connections Help. For information about how to handle events in your app, see Event Handling Guide for iOS.

          --在你的storyboard中,记得将你的outlet、action与相应的view进行链接。

 

 

Displaying Your Views at Runtime               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值