2019独角兽企业重金招聘Python工程师标准>>>

swift 用协议实现代理传值功能

原文  http://blog.csdn.net/whzhaochao/article/details/34903239

1.功能简介

RootViewController中用个lable和一个按钮,点击按钮跳转到模态窗口。在模态窗口中有个TextField和一个按钮,输入文字点击关闭模态按钮后跳转到RootViewController,并改变其label为输入的值。

2 .实现思路

ModelViewController中定义一个成员变量,成员变量有个能改变label值的函数,通过在ModelViewController中调用该函数从而改变RootViewController中label的值,因为ModelViewController自身不能直接改变RootViewController中的成员变量,所以在ModelViewController中定义一个代理,该代理由RootViewControler来实现

3.代码

3.1Protocol.swif

//
//  Protocol.swift
//  modelViewDemo
//
//  Created by 赵超 on 14-6-26.
//  Copyright (c) 2014年 赵超. All rights reserved.
//import Foundation
//协议,定义代理要实现的方法
protocol ModeViewControlDelegate{func changeLabel(newString:String)
}

3.2AppDelegate.swift

//
//  AppDelegate.swift
//  modelViewDemo
//
//  Created by 赵超 on 14-6-26.
//  Copyright (c) 2014年 赵超. All rights reserved.
//import UIKit@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {self.window = UIWindow(frame: UIScreen.mainScreen().bounds)// Override point for customization after application launch.self.window!.backgroundColor = UIColor.whiteColor()self.window!.makeKeyAndVisible()var root=RootViewController()self.window!.rootViewController=rootreturn true}func applicationWillResignActive(application: UIApplication) {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}func applicationDidEnterBackground(application: UIApplication) {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}func applicationWillEnterForeground(application: UIApplication) {// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}func applicationDidBecomeActive(application: UIApplication) {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}func applicationWillTerminate(application: UIApplication) {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}}

3.3RootViewController.swift

//
//  RootViewController.swift
//  modelViewDemo
//
//  Created by 赵超 on 14-6-26.
//  Copyright (c) 2014年 赵超. All rights reserved.
//import UIKit// 实现ModeViewControlDelegate协议
class RootViewController: UIViewController,ModeViewControlDelegate {var btn:UIButton?var label:UILabel?//实现协议中的方法func changeLabel(newString:String){self.label!.text=newString}//按钮事件func btnOnClick(){println("Onclick")var modeView = ModelViewController()//设置modeView中的代理为RootViewController自身modeView.delegate=self//跳转到ModelViewself.presentViewController(modeView,animated: true ,completion: {println("OK")})}override func viewDidLoad() {super.viewDidLoad()self.view.backgroundColor=UIColor.grayColor()label=UILabel()label!.frame=CGRectMake(110,40,100,20)label!.backgroundColor=UIColor.greenColor()label!.text="hello world!"label!.textAlignment = .Centerbtn=UIButton(frame:CGRectMake(110,80,100,20))btn!.backgroundColor=UIColor.greenColor()btn!.setTitle("打开模态",forState:.Normal)btn!.addTarget(self,action:"btnOnClick",forControlEvents: UIControlEvents.TouchUpInside)self.view.addSubview(btn)self.view.addSubview(label)// Do any additional setup after loading the view.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}/*// #pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigationoverride func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.}*/}

3.4ModelViewController.swift

//
//  ModelViewController.swift
//  modelViewDemo
//
//  Created by 赵超 on 14-6-26.
//  Copyright (c) 2014年 赵超. All rights reserved.
//import UIKitclass ModelViewController: UIViewController {var textF:UITextField?//  代理成员变量var delegate:ModeViewControlDelegate?//按钮点击事件func btnOnClick(){var str=textF!.textprintln(str)//调用代理函数,改变Label值self.delegate!.changeLabel(str)//返回RootViewself.dismissModalViewControllerAnimated( true)}override func viewDidLoad() {super.viewDidLoad()view.backgroundColor=UIColor.blueColor()textF=UITextField()textF!.frame=CGRectMake(110,40,100,20)textF!.backgroundColor=UIColor.greenColor()textF!.borderStyle = .RoundedRectvar btn=UIButton(frame:CGRectMake(110,80,100,20))btn.backgroundColor=UIColor.greenColor()btn.setTitle("关闭模态",forState:.Normal)//绑定事件btn.addTarget(self,action:"btnOnClick",forControlEvents: UIControlEvents.TouchUpInside)self.view.addSubview(btn)self.view.addSubview(textF)// Do any additional setup after loading the view.}}

转载于:https://my.oschina.net/fadoudou/blog/699744

swift 用协议实现代理传值功能相关推荐

  1. Swift之UITextField+富文本+代理传值

    ###导语: UITextField有许多好玩好用的地方,因此敝人整理了一些知识点和技巧分享给大家. ####一.初始化 var textField: UITextField!override fun ...

  2. iOS传值之代理传值

    iOS中传值方式有好几种,分别是:代理传值,block传值,属性传值,通知传值,单例传值,利用userdefault或者文件方式传值,通常代理传值和block传值使用最普遍,本文介绍代理传值的方式,后 ...

  3. OS笔记047代理传值和block传值

    在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...

  4. 四大传值详解:属性传值,单例传值,代理传值,block传值

    一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...

  5. C++模拟实现Objective-C协议和代理模式

    Objective-C的协议和代理是运用最多的特性之一,可以说在苹果系列开发中无处不在.事实上很多人都不知道其背后的原理.事实上简单点说,这就是设计模式中的代理模式的经典运用.代理模式简单点说就是为其 ...

  6. iOS笔记UI--委托代理传值

    /* 代理传值思路 1.设置协议方法 2.设置一个ViewController为代理,遵循协议,实现协议方法 3.在点击方法里面,另一个ViewController的委托为这个ViewCOntroll ...

  7. python ip代理池_python实现ip代理池功能示例

    本文实例讲述了python实现ip代理池功能.分享给大家供大家参考,具体如下: 爬取的代理源为西刺代理. 用xpath解析页面 用telnet来验证ip是否可用 把有效的ip写入到本地txt中.当然也 ...

  8. 设置git协议clone代理

    0x0 最近在clone yaffs2仓库时发现clone的异常缓慢,就算开了代理也是,搜索一番发现网上大多都是将设置http.https.ssh协议的代理,对于git协定的代理讲的很少,下面分享下如 ...

  9. iOS开发篇——OC 协议和代理设计模式介绍

    蓝鸥iOS培训讲师推荐:好久没和大家沟通了,没和大家沟通了,今天就和大家说说有关OC内容协议和代理设计模式.首先要讲的是协议 一.协议 OC中的协议和接口有些相似,协议中定义的方法,在类中实现.协议一 ...

最新文章

  1. se(3)-TrackNet: 数据驱动的动态6D物体姿态跟踪, 基于合成域的图片残差校准
  2. 2.34模型--简单字符串查找(占位).c
  3. vs2k5 notes
  4. html5 游戏前景怎么样,独家 HTML5游戏目前究竟怎么样?看完这篇文章,你或许会清晰很多...
  5. 20个 css3 html5 设计工具
  6. 信息学奥赛C++语言:求n个数的和
  7. 设置导航栏的相关属性
  8. SpringCloud工作笔记061---springBoot maven 打包jar报错_serverEndpointExporter
  9. wps如何设置文字环绕图片
  10. Keil MDK使用方法
  11. android zenmode 通知,【Android系统】Android M ZenMode(禅模式)分析(2)
  12. 网络编程:Socket编程从IPv4转向IPv6支持
  13. HTML img src图片路径不存在,则显示一张默认图片的方法
  14. 使用github搭建网站
  15. Navicat如何连接阿里云数据库RDS
  16. 回溯算法 允许重复选择元素的组合
  17. 带你了解无限流量卡的骗局!
  18. 变电站可视化搭建推陈出新?无人值守却更胜一筹
  19. C 语言 宏定义 :字符串化 stringify 的应用
  20. 工信部叫停苹果 Callkit,微信不能直接接听视频了

热门文章

  1. VC++ CryptoAPI最基本编程
  2. C++函数模板Demo - win32 版
  3. Windows 公共控件库研究
  4. C#6.0语言规范(一) 介绍
  5. from表单中 action的后面 内如果是 servlet的虚拟路径的话
  6. SVN如何将版本库url访问地址中的https改为http
  7. [bzoj2186]沙拉公主的困惑
  8. 英文构词法 —— ant、ent 后缀
  9. Fennec Alpha 1 for Windows Mobile available
  10. 3x3,5x5,7x7,9x9卷积核性能比较