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语言的本质(15)——C语言的函数接口入门
C语言的本质(15)--C语言的函数接口 函数的调用者和其实现者之间存在一个协议,在调用函数之前,调用者要为实现者提供某些条件,在函数返回时,实现者完成调用者需要的功能. 函数接口通过函数名,参数和返 ...
- 学习笔记之--MySQL图形界面软件Navicat Premium的安装
最近因项目开发需要,搁置已久的MySQL再次用到.由于以前都是使用命令行进行操作的,没有图形界面.经同学介绍,安装了一个MySQL的图形界面软件.各种数据库的操作也变得直观方便了很多.现在记录下来,一 ...
- 【LeetCode练习题】Minimum Window Substring
找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...
- Unity 之 AfterFixedUpdate,在所有 GameObject FixedUpdate 后执行
目的 FixedUpdate 在不同的 GameObject 互相之间是没有执行顺序的,我们并不能知道哪个 GameObject 先执行 FixedUpdate. 但是,有的时候我们仍然希望某个 Ga ...
- hdu 5623 KK's Number(dp)
问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗104)个数,每次KK都会先拿数.每 ...
- hdu 1035 Robot Motion(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- (原)css 响应式媒体查询 模板
@media only screen and (max-width:340px) { html,input{ font-size:80%; } } @media only screen and (ma ...
- js在线题目
在线题目: https://www.nowcoder.com/ta/js-assessment
- Java中两种实现多线程方式的对比分析
本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...
- Oracle 12c多租户架构浅析
Oracle数据库12c的一大创新即是其采用的多租户架构.对于多租户这项新功能,业内的评价褒贬不一.有的声音认为,这项功能的用处不是特别大,但在某些场景或特定的环境下,多租户依然有它的用处.其最大的用 ...