swift 用協議實現代理傳值功能

個人理解:A通過協議把自身代理給B運行。在B裏面實現代理A協議裏面的方法。以實現A數據操作的目的。

其中,數據的協議方法可以爲閉包。實現自定義數據操作。

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)
}

 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.
    }



}
發佈了14 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章