稍微知道了一点点protocol和delegate的使用。先记一下。
protocol和delegate是用于回调的。比如我们已经从A视图跳转到了B视图,现在用户停留在B视图。好了,现在我们在B视图中进行了某个操作,比如点击了Done按钮,我们需要将用户在B视图中输入的内容传递给A视图。
1. 首先我们在B视图中声明delegate protocol(代理协议)如下,这里的B视图是AddItemViewController视图
protocol AddItemViewControllerDelegate: class
// Define a delegate protocol for object B.
{
func addItemViewControllerDidCancel(controller:AddItemViewController)// when press Cancel
// func addItemViewControllerDidCancel 是ChecklistViewController中的函数
func addItemViewControllerDidDone(controller:AddItemViewController,didFinishiAddingItem item:ChecklistItem)
// func addItemViewController 是ChecklistViewController中的函数
}
// 需要注意的是,这只是一个声明。两个func函数存储在A视图(ChecklistViewController)中,并不会在声明protocol的时候执行。
// 这个delegate protocol的名字叫AddItemViewControllerDelegate。在protocol中包含的函数addItemViewControllerDidCancel和addItemViewControllerDidDone定义在ChecklistViewController方法中。在AddItemViewController中需要修改ChecklistViewController中的数据时,可以使用delegate调用ChecklistViewController中addItemViewControllerDidCancel和addItemViewControllerDidDone函数。同时,需要在ChecklistViewController函数中声明,ChecklistViewController是AddItemViewController的代理。
2. 那么,当B需要用到这个代理A的时候,应该需要怎么调用呢。这里我们还要定义一个optional类型的变量表示AddItemViewControllerDelegate,以便调用。
weakvar delegate:AddItemViewControllerDelegate?
@IBActionfunc cancel(){
delegate?.addItemViewControllerDidCancel(self)
//Make object B send messages to its delegate when something interesting happens
}
4. 我们在A视图中声明A是B的代理,如下,
class ChecklistViewController:UITableViewController,AddItemViewControllerDelegate
// AddItemViewControllerDelegate是AddItemViewController.swift中声明的协议名称。这里表明ChecklistViewController是AddItemViewController的一个delegate(代理)
{
......
}
这里的AddItemViewControllerDelegate是我们在AddItemViewController声明过的protocol的名称。
同时,在ChecklistViewController函数中,我们需要定义被B视图调用的函数,来改变A视图中某些变量。
// 定义AddItemViewController回调ChecklistViewController时会用到的函数addItemViewControllerDidCancel
func addItemViewControllerDidCancel(controller:AddItemViewController) {
dismissViewControllerAnimated(true, completion:nil)
}
// 定义AddItemViewController回调ChecklistViewController时会用到的函数addItemViewControllerDidDone
func addItemViewControllerDidDone(controller:AddItemViewController, didFinishiAddingItem item:ChecklistItem) {
let newRowIndex = items.count
items.append(item)
let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
let indexPaths = [indexPath]
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
dismissViewControllerAnimated(true, completion:nil)
}
5. 告诉B,A已经是你的代理了。最好告诉B的地方是在我们之前讲到的prepareForSegue方法中。如下
overridefunc prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?)
{
if segue.identifier =="AddItem"{
// segue's identifier
//2
// the segue does not go directly to AddItemViewController but to the navigation controller that embeds it.
let navigationController = segue.destinationViewControlleras!UINavigationController
//3
let controller = navigationController.topViewControlleras!AddItemViewController
//4
controller.delegate =self
}
elseif segue.identifier =="EditItem"
{
let navigationController = segue.destinationViewControlleras!UINavigationController
let controller = navigationController.topViewControlleras!AddItemViewController
// The view controller at the top of the navigation stack.获取需要的ViewController
controller.delegate =self
iflet indexPath =tableView.indexPathForCell(senderas!UITableViewCell)
{
controller.itemToEdit =items[indexPath.row]
}
}
}
书中还提到使用代理协议的五个步骤:
Delegates in five easy steps
These are the steps for setting up the delegate pattern between two objects,where object A is the delegate for object B, and object B will send messagesback to A. The steps are:
1. Define a delegateprotocolfor object B. //在B中定义代理协议
2. Give object B an optional delegatevariable. This variable should beweak.
3. Make object B send messages to its delegate when something interestinghappens, such as the user pressing the Cancel or Done buttons, or when itneeds a piece of information. You writedelegate?.methodName(self, . . .)
4. Make object A conform to the delegate protocol. It should put the name of the protocol in its classline and implement the methods from the protocol.
5. Tell object B that object A is now its delegate.
以上。