Swift: Connect the UI to Source Code


Swift: Connect the UI to Source Code


View controllers implement your app’s behavior. A view controller manages a single content view with its hierarchy of subviews. View controllers coordinate the flow of information between the app’s data model, which encapsulates the app’s data, and the views that display that data, manage the life cycle of their content views, handle orientation changes when the device is rotated, define the navigation within your app, and implement the behavior to respond to user input.

View controller负责实现APP的功能。一个view controller管理一个content view以及具备一定层次结构的subview。View controller对APP中负责封装数据的data model与负责显示数据的view之间的信息流动进行协调,管理content view的生命周期、处理设备的旋转事件、定义APP内的导航、以及对用户的输入进行响应。


Outlets provide a way to reference interface objects—the objects you added to your storyboard—from source code files. To create an outlet, Control-drag from a particular object in your storyboard to a view controller file. This operation creates a property for the object in your view controller file, which lets you access and manipulate that object from code at runtime.

Outlet提供了一种在运行时通过代码操纵用户界面中对象的方法,这些对象都是由你添加到storyboard中的。要创建一个outlet,只需要按下Control并拖拽一个storyboard中特定的对象到view controller文件中。这个操作会在view controller中创建一个property,通过这个property你就可以在运行时使用代码来操纵这个特定对象。


An action (or an action method) is a piece of code that’s linked to an event that can occur in your app. When that event takes place, the code gets executed. You can define an action method to accomplish anything from manipulating a piece of data to updating the UI. You use actions to drive the flow of your app in response to user or system events.