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.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值