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=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//跳转到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 = .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://www.cnblogs.com/Free-Thinker/p/5007362.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419252.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1685):前端系列实战课程之设置难度

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>游戏初始化界面</title><style>body {ma…

git 学习

参考 http://git.oschina.net/oschina/git-osc/wikis/%E5%B8%AE%E5%8A%A9#继续阅读 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 常用git 命令 git initgit add <file>git commit -am "XXXX"git remote add origin g…

Linux如何通过命令查看日志文件的某几行(中间几行或最后几行)

linux 如何显示一个文件的某几行(中间几行) 【一】从第3000行开始&#xff0c;显示1000行。即显示3000~3999行 cat filename | tail -n 3000 | head -n 1000 【二】显示1000行到3000行 cat filename| head -n 3000 | tail -n 1000 *注意两种方法的顺序 分解&#xff1a; tail -…

Linux日志高频使用命令

1、cat&#xff1a; 功能&#xff1a;1&#xff09;显示整个文件 $ cat fileName 2&#xff09;把文件串连接后传到基本输出&#xff0c;如将几个文件合并为一个文件或输出到屏幕。 $ cat file1 file2 > file 2、more&#xff1a;以百分比的形式查看日志。 3、less&am…

技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)

一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账&#xff0c;如PO收货、发票验证(LIV)、工单发料、向生产车间发料等等。不用说&#xff0c;一定需要在IMG中进行配置才可以实现自动处理。但SAP实现的这种自动配置的机制是怎样的呢&#xff1f;其实也并不复杂&…

Java多线程系列--“JUC锁”05之 非公平锁

转载自&#xff1a;http://www.cnblogs.com/skywang12345/p/3496651.html点击打开链接 概要 前面两章分析了"公平锁的获取和释放机制"&#xff0c;这一章开始对“非公平锁”的获取锁/释放锁的过程进行分析。内容包括&#xff1a; 参考代码 获取非公平锁(基于JDK1.7.0…

空间点到直线的距离

作者&#xff1a;zdd出处&#xff1a;http://www.cnblogs.com/graphics/ 本文版权归作者和博客园共有&#xff0c;欢迎转载&#xff0c;但未经作者同意必须保留此段声明&#xff0c;且在文章页面明显位置给出原文连接&#xff0c;否则保留追究法律责任的权利.转载于:https://ww…

TCP面向连接中的“连接”和“可靠”与“不可靠”

转载自&#xff1a;http://blog.csdn.net/haizhongyun/article/details/7621199点击打开链接 连接是对状态的保持 实际上就是在客户端和服务器端都维护一个变量&#xff0c;这个变量维护现在数据传输的状态&#xff0c;例如传输了哪些数据&#xff0c;下一次需要传输哪些数据…

UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout

1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法&#xff0c;这样可以拿到 UIGraphicsGetCurrentContext&#xff0c;就可以画画了。而setNeedsLayout会默认调用layoutSubViews&#xff0c; 就可以 …

深入理解Java中为什么内部类可以访问外部类的成员

转载自&#xff1a;http://blog.csdn.net/zhangjg_blog/article/details/20000769 内部类简介 虽然Java是一门相对比较简单的编程语言&#xff0c;但是对于初学者&#xff0c; 还是有很多东西感觉云里雾里&#xff0c; 理解的不是很清晰。内部类就是一个经常让初学者感到迷惑的…

JMS与Spring之二(用message listener container异步收发消息)

转自&#xff1a;http://blog.csdn.net/moonsheep_liu/article/details/6684948转载于:https://www.cnblogs.com/septemberlxc/p/5016275.html

ASP.NET MVC5+EF6+EasyUI 后台管理系统(51)-系统升级

系统很久没有更新内容了&#xff0c;期待已久的更新在今天发布了&#xff0c;最近花了2个月的时间每天一点点&#xff0c;从原有系统 MVC4EF5UNITY2.XQuartz 2.0easyui 1.3.4无缝接入 MVC5EF6Unity4.xQuartz 2.3 easyui 1.4.3. 并以easyui 1.4.3的gray皮肤为基础&#xff0c;升…