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为何 ...
随机推荐
- Chapter 4. Button, Checkbutton, and Radiobutton Widgets 按钮,复选按钮,单选按钮
Chapter 4. Button, Checkbutton, and Radiobutton Widgets 按钮,复选按钮,单选按钮 几乎所有的Perl/Tk 应用使用按钮以这样或者那样的方式 ...
- leetcode_question_104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- hdu 5615 Jam's math problem(判断是否能合并多项式)
方法一:由十字相乘相关理论我们能知道,如果要有p,k,q,m,那么首先要有解,所以b*b-4*a*c要>0,然而因为p,k,q,m是正整数,所以代表x1,x2都是有理数,有理数是什么鬼呢?就是解 ...
- 【二进制拆分多重背包】【HDU1059】【Dividing】
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- android开发MD5加密工具类(一)
MD5加密工具类整理: package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.secu ...
- asp.net 两个页面之前传递数据
.在两个表单之间传递数据 看下面的代码: 对于WebForm1: private void Page_Load(object sender, System.EventArgs e) { ArrayLi ...
- CentOS 6 安装 python and pip
yum update yum list python* //查看可安装python包 yum install python34.i686_64 wget https://bootstrap.p ...
- 记一次dedeCMS网站搭建全过程
Step 1 使用阿里云Windows Server 2012服务器 { 使用远程桌面进行操作,ip admin pwd登录 } Step 2 下载安装phpStudy包 { 下载安装,直接安装到C盘 ...
- send()和recv()函数详解
send()函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP连 ...
- 关于lvs+keepalived只加入一台realserver问题
今天做lvs+keepalived+mysql项目实施方案,在配置lvs+keepalived时都是ok的,但是就只加入第一台realserver.本人也感到很奇怪,lvs+keepalived本人也 ...