天天看點

Start Developing iOS Apps (Swift) 學習筆記 (2)

四、Work with View Controllers

1、

UIViewController

 methods get called as follows:

  • viewDidLoad()

    —Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. This method is intended for initial setup. However, because views may be purged due to limited resources in an app, there is no guarantee that it will be called only once.
  • viewWillAppear()

    —Intended for any operations that you want always to occur before the view becomes visible. Because a view’s visibility may be toggled or obscured by other views, this method is always called immediately before the content view appears onscreen.
  • viewDidAppear()

    —Intended for any operations that you want to occur as soon as the view becomes visible, such as fetching data or showing an animation. Because a view’s visibility may be toggled or obscured by other views, this method is always called immediately after the content view appears onscreen. 

    A complementary set of teardown methods exists, as shown in the state transition diagram above. 

    (沒明白,待更新)

2、This style of app design where view controllers serve as the communication pipeline between your views and data model is known as   MVC (Model-View-Controller). In this pattern, models keep track of your app’s data, views display your user interface and make up the content of an app, and controllers manage your views. By responding to user actions and populating views with content from the   data model, controllers serve as a gateway for communication between the model and views. MVC is central to a good design for any iOS app, and so far, the FoodTracker app has been built along MVC principles. (MVC是指view controllers作為views和data model的通信管道。Model儲存app的資料,View顯示UI,并組成app的内容,controller管理views,裝載、解除安裝等)

3、

UIImagePickerControllerDelegate

:

  1. func imagePickerControllerDidCancel(picker: UIImagePickerController)

  2. func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])

The first of these, 

imagePickerControllerDidCancel(_:)

, gets called when a user taps the image picker’s Cancel button. This method gives you a chance to dismiss the 

UIImagePickerController

 (and optionally, do any necessary cleanup). Implement 

imagePickerControllerDidCancel(_:)

 to do that. (imagePickerControllerDidCancel,當使用者點選iamge picker的cancel按鈕時,該方法被調用)

imagePickerController(_:didFinishPickingMediaWithInfo:)

, gets called when a user selects a photo. This method gives you a chance to do something with the image or images that a user selected from the picker.(imagePickerController,當使用者選擇一個圖檔時,該方法被調用)

五、Create a Table View

1、To display dynamic data, a table view needs two important helpers: a data source and a delegate. A table view   data source, as implied by its name, supplies the table view with the data it needs to display. A table view   delegate  helps the table view manage cell selection, row heights, and other aspects related to displaying the data. By default,  

UITableViewController

 and its subclasses adopt the necessary protocols to make the table view controller both a data source (

UITableViewDataSource

 protocol) and a delegate (

UITableViewDelegate

 protocol) for its associated table view. (table view需要兩個重要的輔助:資料源和代理。預設情況下,

UITableViewController

 及其子類遵循兩個協定,一個是UITableViewDataSource協定,即資料源協定;另一個是UITableViewDelegate協定,機代理協定)

   A functioning table view requires three table view data source methods.

  1. func numberOfSectionsInTableView(tableView: UITableView) -> Int

  2. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

  3. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

(numberOfSectionsInTableView()方法,指定table view需要顯示幾個section。

tableView(_:numberOfRowsInSection:)方法,指定table view 在指定的section上顯示多少row。

tableView(_:cellForRowAtIndexPath:)方法,提供一個配置的cell顯示在指定的row上。

六、 Implement Navigation

1、Transitions between scenes are called   segues。 (segues是指scenes之間的轉換)

2、Whenever a segue gets triggered, it provides a place for you to add your own code that gets executed. This method is called  

prepareForSegue(_:sender:)

, and it gives you a chance to store data and do any necessary cleanup on the   source view controller  (the view controller that the segue is coming from). (當一個segue被觸發時,prepareForSegue(_:sender:)方法将被觸發執行,可以在這個方法中添加資料存儲等邏輯功能。source view controller是一個view controller,它的内容在segue開始時被顯示出來)

七、Implement Edit and Delete Behavior

1、

prepareForSegue(_:sender:)

 method is called before any segue gets executed.

2、

tableView(_:commitEditingStyle:forRowAtIndexPath:): 

This delegate method is in charge of managing the table rows when it’s in editing mode.

    tableView(_:canEditRowAtIndexPath:): 

support editing.  

八、Persist Data

1、   Convenience initializers  are secondary, supporting initializers that need to call one of their class’s   designated initializers.   Designated initializers are the primary initializers for a class. They fully initialize all properties introduced by that class and call a   superclass  initializer to continue the initialization process up the superclass chain.