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 用协议实现代理传值功能的更多相关文章

  1. 利用Swift之协议语法实现页面间的传值功能

    随着Swift 新开发语言的发布,又随着Xcode6.0.1的正式发布,利用swift编写iOS代码迫在眉睫,笔者在使用Objective-C开发近三年以来,对这种优雅的语法深感赞叹,下面我将对比式的 ...

  2. Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值

    一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...

  3. iOS 开发之协议-代理传值

    刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状态,不知道两个UIViewController之间怎么进行传值. 面试过几个童鞋,问道怎么用 del ...

  4. swift -NavigationController,代理传值

    // // ViewController.swift // NavigationController // import UIKit import Foundation class ViewContr ...

  5. IOS学习[Swift中跳转与传值]

    Swift中页面跳转与传值: 1.简单方式 首先,Swift的跳转可分为利用xib文件跳转与storyboard跳转两种方法,我这里选择使用storyboard的界面跳转方法. 1.通过在storyb ...

  6. 窥探Swift之协议(Protocol)和委托代理(Delegate)回调的使用

    协议与委托代理回调在之前的博客中也是经常提到和用到的在<Objective-C中的委托(代理)模式>和<iOS开发之窥探UICollectionViewController(四) - ...

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

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

  8. View 与 Controller 之间的delegate(代理)传值

    这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...

  9. 【转】fiddler-http协议调试代理工具

    题目有一些激进.但是在前端界打滚了这么多年,fiddler一直都是陪着我走过来了.它就是一个抓包神奇,代理神器.它的厉害之处,我简单地说一下,希望你们看了以后,能点上32个赞. 1.fiddler为何 ...

随机推荐

  1. HDOJ-1007 Quoit Design(最近点对问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具 实际就是要求最近的两个坐标的距离 ...

  2. 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  3. Makefile中使用$$的使用

      http://blog.csdn.net/darennet/article/details/8185881   Makefile中使用$$的使用     在makefile中,会经常使用shell ...

  4. Sereja and Coat Rack(水)

    Sereja and Coat Rack Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  5. Sumsets(3sum问题,枚举d,c二分a+b)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9997   Accepted: 2736 Descripti ...

  6. Rescue(bfs)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  7. hdu 4775 Infinite Go(暴力)

    pid=4775" target="_blank" style="">题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋 ...

  8. python Debug 单步调试

    一直犯愁的是python的调试,曾经写c都是编译完了用gdb直接调试了,轻松愉快.如今遇到这么一个解释型的程序.不知道怎么办了.用log吧,有时就是一个小程序,不想写这么多代码.打屏吧.有时屏幕翻得快 ...

  9. CSS3中的弹性流体盒模型技术详解

    先回顾一下CSS1 和 CSS2中都已经定义了哪些布局方面的属性,这样也会增加我们理解弹性布局.   其实我们现在有很多一部分人,你们刚刚接触CSS层叠样式表,或者接触有一段时间了,但是却没有很好的去 ...

  10. Java中两种实现多线程方式的对比分析

    本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...