Swift - 创建代理协议实现页面间参数传递和方法调用

前端之家收集整理的这篇文章主要介绍了Swift - 创建代理协议实现页面间参数传递和方法调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在开发中,经常需要用到协议代理模式。比如,进入编辑页面修改数据后,将新数据回传到主界面。

下面通过一个样例来说明协议代理模式,功能如下:
1,主页面有一个标签和一个修改按钮,点击修改按钮会跳转到编辑页面
2,编辑页面修改输入框的文字后,点击“确定”则返回主页面,同时主页面标签值会替换成新值
3,如果点击编辑页面的“取消”按钮则直接返回主页面

效果图如下:


实现过程:
1,首先在storyboard中绘制如下两个界面,同时将主界面的“修改”按钮与编辑页做present modally关联


2,设置这个关联Segue的Identity为 EditView


3,主界面ViewController.swift
@H_403_64@import @H_403_64@UIKit
@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@override @H_403_64@func @H_403_64@viewDidLoad() {
@H_403_64@ @H_403_64@super @H_403_64@.viewDidLoad()
@H_403_64@ @H_403_64@// Do any additional setup after loading the view,typically from a nib.
@H_403_64@}
@H_403_64@ @H_403_64@override @H_403_64@func @H_403_64@didReceiveMemoryWarning() {
@H_403_64@ @H_403_64@super @H_403_64@.didReceiveMemoryWarning()
@H_403_64@ @H_403_64@// Dispose of any resources that can be recreated.
@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@if @H_403_64@segue.identifier == @H_403_64@"EditView" @H_403_64@{
@H_403_64@ @H_403_64@//通过seque的标识获得跳转目标
@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@ @H_403_64@//设置代理
@H_403_64@controller.delegate = @H_403_64@self
@H_403_64@ @H_403_64@//将值传递给新页面
@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@ @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_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@ @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@import @H_403_64@UIKit
@H_403_64@class @H_403_64@EditViewController @H_403_64@: @H_403_64@UIViewController @H_403_64@{
@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@var @H_403_64@delegate: @H_403_64@EditViewControllerDelegate @H_403_64@?
@H_403_64@
@H_403_64@ @H_403_64@var @H_403_64@oldInfo: @H_403_64@String @H_403_64@?
@H_403_64@
@H_403_64@ @H_403_64@override @H_403_64@func @H_403_64@viewDidLoad() {
@H_403_64@ @H_403_64@super @H_403_64@.viewDidLoad()
@H_403_64@ @H_403_64@// Do any additional setup after loading the view.
@H_403_64@ @H_403_64@if @H_403_64@oldInfo != @H_403_64@nil @H_403_64@{
@H_403_64@textField.text = oldInfo
@H_403_64@}
@H_403_64@}
@H_403_64@ @H_403_64@override @H_403_64@func @H_403_64@didReceiveMemoryWarning() {
@H_403_64@ @H_403_64@super @H_403_64@.didReceiveMemoryWarning()
@H_403_64@ @H_403_64@// Dispose of any resources that can be recreated.
@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@delegate?.editInfo( @H_403_64@self @H_403_64@,newInfo: textField.text!)
@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@delegate?.editInfoDidCancer( @H_403_64@self @H_403_64@)
@H_403_64@}
@H_403_64@}

5,编辑页代理 EditViewControllerDelegate.swift
@H_403_64@protocol @H_403_64@EditViewControllerDelegate @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@ @H_403_64@func @H_403_64@editInfoDidCancer(controller: @H_403_64@EditViewController @H_403_64@)
@H_403_64@}

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的该方法

猜你在找的Swift相关文章