swift 用协议实现代理传值功能

1.功能简介

RootViewController中用个lable和一个按钮,点击按钮跳转到模态窗口。在模态窗口中有个TextField和一个按钮,输入文字点击关闭模态按钮后跳转到RootViewController,并改变其label为输入的值。




2.实现思路

ModelViewController中定义一个成员变量,成员变量有个能改变label值的函数,通过在ModelViewController中调用该函数从而改变RootViewController中label的值,因为ModelViewController自身不能直接改变RootViewController中的成员变量,所以在ModelViewController中定义一个代理,该代理由RootViewControler来实现


3.代码

3.1Protocol.swif

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  Protocol.swift  
  3. //  modelViewDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-26.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import Foundation  
  10. //协议,定义代理要实现的方法  
  11. protocol ModeViewControlDelegate{  
  12.     func changeLabel(newString:String)  
  13. }  



3.2AppDelegate.swift


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  AppDelegate.swift  
  3. //  modelViewDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-26.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8. 原文来源 http://blog.csdn.net/whzhaochao/article/details/34903239 
  9.  
  10. import UIKit  
  11.   
  12. @UIApplicationMain  
  13. class AppDelegate: UIResponder, UIApplicationDelegate {  
  14.                               
  15.     var window: UIWindow?  
  16.   
  17.   
  18.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {  
  19.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  20.         // Override point for customization after application launch.  
  21.         self.window!.backgroundColor = UIColor.whiteColor()  
  22.         self.window!.makeKeyAndVisible()  
  23.         var root=RootViewController()  
  24.           
  25.         self.window!.rootViewController=root  
  26.           
  27.         return true  
  28.     }  
  29.   
  30.     func applicationWillResignActive(application: UIApplication) {  
  31.         // 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.  
  32.         // 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.  
  33.     }  
  34.   
  35.     func applicationDidEnterBackground(application: UIApplication) {  
  36.         // 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.  
  37.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  38.     }  
  39.   
  40.     func applicationWillEnterForeground(application: UIApplication) {  
  41.         // 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.  
  42.     }  
  43.   
  44.     func applicationDidBecomeActive(application: UIApplication) {  
  45.         // 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.  
  46.     }  
  47.   
  48.     func applicationWillTerminate(application: UIApplication) {  
  49.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  50.     }  
  51.   
  52.   
  53. }  


3.3RootViewController.swift




[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  RootViewController.swift  
  3. //  modelViewDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-26.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11.   
  12.   
  13. // 实现ModeViewControlDelegate协议  
  14. class RootViewController: UIViewController,ModeViewControlDelegate {  
  15.     
  16.       
  17.     var btn:UIButton?  
  18.     var label:UILabel?  
  19.       
  20.     //实现协议中的方法  
  21.     func changeLabel(newString:String){  
  22.         self.label!.text=newString  
  23.     }  
  24.     //按钮事件  
  25.     func btnOnClick(){  
  26.         println("Onclick")  
  27.           
  28.         var modeView = ModelViewController()  
  29.         //设置modeView中的代理为RootViewController自身  
  30.         modeView.delegate=self  
  31.         //跳转到ModelView  
  32.         self.presentViewController(modeView,  
  33.             animatedtrue ,  
  34.             completion: {  
  35.                 println("OK")  
  36.             })  
  37.           
  38.     }  
  39.     override func viewDidLoad() {  
  40.         super.viewDidLoad()  
  41.         self.view.backgroundColor=UIColor.grayColor()  
  42.           
  43.         label=UILabel()  
  44.         label!.frame=CGRectMake(110,40,100,20)  
  45.         label!.backgroundColor=UIColor.greenColor()  
  46.         label!.text="hello world!"  
  47.         label!.textAlignment = .Center  
  48.           
  49.         btn=UIButton(frame:CGRectMake(110,80,100,20))  
  50.         btn!.backgroundColor=UIColor.greenColor()  
  51.         btn!.setTitle("打开模态",forState:.Normal)  
  52.         btn!.addTarget(self,action:"btnOnClick",forControlEvents: UIControlEvents.TouchUpInside)  
  53.           
  54.         self.view.addSubview(btn)  
  55.         self.view.addSubview(label)  
  56.   
  57.         // Do any additional setup after loading the view.  
  58.     }  
  59.   
  60.     override func didReceiveMemoryWarning() {  
  61.         super.didReceiveMemoryWarning()  
  62.         // Dispose of any resources that can be recreated.  
  63.     }  
  64.       
  65.   
  66.     /* 
  67.     // #pragma mark - Navigation 
  68.  
  69.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  70.     override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { 
  71.         // Get the new view controller using [segue destinationViewController]. 
  72.         // Pass the selected object to the new view controller. 
  73.     } 
  74.     */  
  75.   
  76. }  

3.4ModelViewController.swift


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ModelViewController.swift  
  3. //  modelViewDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-26.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11.   
  12.   
  13. class ModelViewController: UIViewController {  
  14.     var textF:UITextField?  
  15.     //  代理成员变量  
  16.     var delegate:ModeViewControlDelegate?  
  17.       
  18.     //按钮点击事件  
  19.     func btnOnClick(){  
  20.         var str=textF!.text  
  21.         println(str)  
  22.         //调用代理函数,改变Label值  
  23.          self.delegate!.changeLabel(str)  
  24.    
  25.         //返回RootView  
  26.         self.dismissModalViewControllerAnimatedtrue)  
  27.   
  28.     }  
  29.   
  30.     override func viewDidLoad() {  
  31.         super.viewDidLoad()  
  32.           
  33.         view.backgroundColor=UIColor.blueColor()  
  34.           
  35.           
  36.         textF=UITextField()  
  37.         textF!.frame=CGRectMake(110,40,100,20)  
  38.         textF!.backgroundColor=UIColor.greenColor()  
  39.         textF!.borderStyle = .RoundedRect  
  40.          
  41.           
  42.         var btn=UIButton(frame:CGRectMake(110,80,100,20))  
  43.         btn.backgroundColor=UIColor.greenColor()  
  44.         btn.setTitle("关闭模态",forState:.Normal)  
  45.         //绑定事件  
  46.         btn.addTarget(self,action:"btnOnClick",forControlEvents: UIControlEvents.TouchUpInside)  
  47.           
  48.         self.view.addSubview(btn)  
  49.         self.view.addSubview(textF)  
  50.           
  51.   
  52.         // Do any additional setup after loading the view.  
  53.     }  
  54.   
  55.   
  56.   
  57. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值