zoukankan      html  css  js  c++  java
  • 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.
        }
    
    
    
    }
  • 相关阅读:
    Atitit 安全措施流程法 目录 1. 常见等安全措施方法 2 1.1. 安全的语言 代码法,编译型 java 2 1.2. 安全编码法 2 1.3. 安全等框架类库 api 2 1.4. 加密法
    Atitit api与安全措施法 目录 1.1. 模板替换 sprintf %f %d 数字小数字段格式化转换校验法 1 2.  $pdo->exec 与query 2 2.1. 数字校
    Atitit 安全审计法 目录 1. 安全审计数据结构 1 2. Expame 提现流程 1 2.1. 获取提现钱的数据余额 1 2.2. 扣去余额 1 2.3. 开始safe log 2 2.4.
    Atitit 防注入 sql参数编码法 目录 1.2. 提升可读性pg_escape_literal — 转义文字以插入文本字段 1 1.2.1. 说明 1 1.3. 推荐pg_escape_str
    Atitit aes 加密法php实现
    Atitit 登录票据安全法 目录 1.1. cookie对象规范 1 1.2. Cookie加解密 1 1.3. Cookie密文动态更换,根据一个时间3天比如 1 1.4. 服务端撤销key 1
    atitit 2010 2010 diary log events memorabilia v4 tbb No finish , wait to finish ***Mon8-11 cant
    Atitit 安全流程法 目录 1. 常见等安全措施方法 2 1.1. 安全的语言 代码法,编译型 java 2 1.2. 安全编码法 2 1.3. 安全等框架类库 api 2 1.4. 加密法 2
    Atitit 数据查询法 目录 1. 数据查询语言QL (推荐) 1 1.1. Sql 1 1.2. 对象查询语言(OQL) 1 1.3. Atitit QL查询语言总结Jpql Ongl
    Atitit json数据查询法 jsonpath 目录 1.1. 1.概述 1 1.2. 3.2。经营者特殊符号 1 1.3. # JSONPath expressions 2 1.4. Xpa
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5007741.html
Copyright © 2011-2022 走看看