swift 用协议实现代理传值功能
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=root return 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
//跳转到ModelView
self.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 = .Center 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(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 navigation
override 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 UIKit class ModelViewController: UIViewController {
var textF:UITextField?
// 代理成员变量
var delegate:ModeViewControlDelegate? //按钮点击事件
func btnOnClick(){
var str=textF!.text
println(str)
//调用代理函数,改变Label值
self.delegate!.changeLabel(str) //返回RootView
self.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 = .RoundedRect var 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.
} }
swift 用协议实现代理传值功能的更多相关文章
- 利用Swift之协议语法实现页面间的传值功能
随着Swift 新开发语言的发布,又随着Xcode6.0.1的正式发布,利用swift编写iOS代码迫在眉睫,笔者在使用Objective-C开发近三年以来,对这种优雅的语法深感赞叹,下面我将对比式的 ...
- Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...
- iOS 开发之协议-代理传值
刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状态,不知道两个UIViewController之间怎么进行传值. 面试过几个童鞋,问道怎么用 del ...
- swift -NavigationController,代理传值
// // ViewController.swift // NavigationController // import UIKit import Foundation class ViewContr ...
- IOS学习[Swift中跳转与传值]
Swift中页面跳转与传值: 1.简单方式 首先,Swift的跳转可分为利用xib文件跳转与storyboard跳转两种方法,我这里选择使用storyboard的界面跳转方法. 1.通过在storyb ...
- 窥探Swift之协议(Protocol)和委托代理(Delegate)回调的使用
协议与委托代理回调在之前的博客中也是经常提到和用到的在<Objective-C中的委托(代理)模式>和<iOS开发之窥探UICollectionViewController(四) - ...
- OS笔记047代理传值和block传值
在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...
- View 与 Controller 之间的delegate(代理)传值
这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...
- 【转】fiddler-http协议调试代理工具
题目有一些激进.但是在前端界打滚了这么多年,fiddler一直都是陪着我走过来了.它就是一个抓包神奇,代理神器.它的厉害之处,我简单地说一下,希望你们看了以后,能点上32个赞. 1.fiddler为何 ...
随机推荐
- C语言的本质(30)——C语言与汇编之ELF文件格式
ELF(Executable and Linking Format)文件格式是一个开放标准,各种UNIX系统的可执行文件都采用ELF格式,ELF是一种对象文件的格式,用于定义不同类型的对象文件(Obj ...
- cdoj 491 Tricks in Bits
//无脑爆居然能过!!!!! 解:其实正解也是暴力,但是可以证明在n>6时答案一定为零. 第一步:对于任意两个数他们的二进制数要么有一半+的位是相同的,要么有一半+的位是不同的,于是首先使用与运 ...
- 【hihocoder 1258 Osu! Master】
2015北京区域赛现场赛签到题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
- poj 3753 Training little cats_矩阵快速幂
题意: 通过各种操作进行,给第i只猫花生,第i只猫吃光花生,第i只猫和第j只猫互换花生,问n次循环操作后结果是什么 很明显是构建个矩阵,然后矩阵相乘就好了 #include <iostream& ...
- Linux内核中常见内存分配函数(二)
常用内存分配函数 __get_free_pages unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) __get_f ...
- Java Collection 集合类大小调整带来的性能消耗
Java Collection类的某些详细实现因为底层数据存储基于数组,随着元素数量的添加,调整大小的代价非常大.随着Collection元素增长到某个上限,调整其大小可能出现性能问题. 当Colle ...
- 第九十六题(编写strcpy 函数)
96.08 年中兴校园招聘笔试题 1.编写strcpy 函数 已知strcpy 函数的原型是 char *strcpy(char *strDest, const char *strSrc); 当中st ...
- Oracle 归档路径
Oracle 的归档路径设置,这里主要按照官网说明记录 LOG_ARCHIVE_DEST_n 与 LOG_ARCHIVE_DEST_STATE_n 这两个参数. 我使用的数据库是11.2版本,这两个参 ...
- ios百度地图不能定位问题
在IOS8中定位功能新增了两个方法: - (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE ...
- Marshal 类的内存操作的一般功能
Marshal类 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法. 命名空间:System.Runtim ...