下面通过一个样例来说明协议代理模式,功能如下:
效果图如下:
实现过程:
1,首先在storyboard中绘制如下两个界面,同时将主界面的“修改”按钮与编辑页做present modally关联
2,设置这个关联Segue的Identity为 EditView
3,主界面ViewController.swift
|
@H_403_64@class
@H_403_64@ViewController
@H_403_64@:
@H_403_64@UIViewController
@H_403_64@,
@H_403_64@EditViewControllerDelegate
@H_403_64@{
@H_403_64@
@H_403_64@@IBOutlet
@H_403_64@weak
@H_403_64@var
@H_403_64@label:
@H_403_64@UILabel
@H_403_64@!
@H_403_64@
@H_403_64@}
@H_403_64@}
@H_403_64@
@H_403_64@
@H_403_64@override
@H_403_64@func
@H_403_64@prepareForSegue(segue:
@H_403_64@UIStoryboardSegue
@H_403_64@,sender:
@H_403_64@AnyObject
@H_403_64@?) {
@H_403_64@
@H_403_64@let
@H_403_64@controller = segue.destinationViewController
@H_403_64@as
@H_403_64@!
@H_403_64@EditViewController
@H_403_64@controller.oldInfo = label.text
@H_403_64@}
@H_403_64@}
@H_403_64@
@H_403_64@func
@H_403_64@editInfo(controller:
@H_403_64@EditViewController
@H_403_64@,newInfo:
@H_403_64@String
@H_403_64@){
@H_403_64@label.text = newInfo;
@H_403_64@controller.presentingViewController!.dismissViewControllerAnimated(
@H_403_64@true
@H_403_64@,completion:
@H_403_64@nil
@H_403_64@)
@H_403_64@}
@H_10_301@
@H_403_64@
@H_403_64@
@H_403_64@func
@H_403_64@editInfoDidCancer(controller:
@H_403_64@EditViewController
@H_403_64@){
@H_403_64@controller.presentingViewController!.dismissViewControllerAnimated(
@H_403_64@true
@H_403_64@,completion:
@H_403_64@nil
@H_403_64@)
@H_403_64@}
@H_403_64@}
|
4,编辑页 EditViewController.swift
|
@H_403_64@
@H_403_64@@IBOutlet
@H_403_64@weak
@H_403_64@var
@H_403_64@textField:
@H_403_64@UITextField
@H_403_64@!
@H_403_64@
@H_403_64@
@H_403_64@
@H_403_64@textField.text = oldInfo
@H_403_64@}
@H_403_64@}
@H_403_64@}
@H_403_64@
@H_403_64@@IBAction
@H_403_64@func
@H_403_64@done(sender:
@H_403_64@AnyObject
@H_403_64@) {
@H_403_64@}
@H_403_64@
@H_403_64@@IBAction
@H_403_64@func
@H_403_64@cancel(sender:
@H_403_64@AnyObject
@H_403_64@) {
@H_403_64@}
@H_403_64@}
|
5,编辑页代理 EditViewControllerDelegate.swift
6,在Storyboard中,将编辑页的Class设为EditViewController presentedViewController:The view controller that is presented by this view controlller(read-only),被本视图控制器present出来的的视图控制器
presentingViewController:The view controller that presented this view controller. (read-only),present出来本视图控制器的视图控制器
如A-->弹出B,则A.presentedViewController = B
B.presentingViewController = A
dismissViewControllerAnimated:YES
Dismisses the view controller that was presented modally by the view controller.
也就是在在A上调该方法,dismiss掉A弹出的vc
如果在B上调,会调用presenting view的该方法,即A的该方法