1. 程式人生 > >Swift如何實現代理,block傳值

Swift如何實現代理,block傳值

要實現代理傳值,我們先來看一下需求
這裡寫圖片描述這裡寫圖片描述
通過點選提交進入第二個頁面,點選返回pop到第一個頁面,並且用代理和block
傳回一個值.
我們先來看第二個頁面的程式碼:

import UIKit
//定義協議
protocol loginDelegate{
    //無參無返回值代理方法
    func test()
    //有參無返回值方法
    func test2(name:String)
    //有兩個引數無返回值代理方法
    func test3(name:String,age:Int)
    //有參有返回值代理方法(返回兩個值)
    func test4(name:String
,age:Int)->(String,Int) } //定義block(有參無返回值) typealias block = (str:String)->Void class LoginViewController: UIViewController { //定義屬性 var name:String! var delegate:loginProtocol! var myBlock:block! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor =
UIColor.whiteColor() print(name) let btn:UIButton = UIButton(type: UIButtonType.System) btn.frame = CGRectMake(20, 100, 300, 30) btn.setTitle("返回", forState: UIControlState.Normal) btn.backgroundColor = UIColor.cyanColor() btn.addTarget(self, action: "backBtnClicked"
, forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(btn) } //點選返回按鈕實現方法 func backBtnClicked() { //呼叫代理方法,並且賦值 self.delegate.test() self.delegate.test2("我是代理傳過來的值") self.delegate.test3("teddy", age: 20) self.delegate.test4("Andy", age: 18) self.myBlock(str:"我是block傳值") self.navigationController?.popViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

ViewController.swift頁面的程式碼:

import UIKit

//遵守協議loginDelagate
class ViewController: UIViewController,loginDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let label:UILabel = UILabel.init()
        label.frame = CGRectMake(20, 80, 100, 30)
        label.text = "姓名:"
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.blueColor()
        label.font = UIFont.systemFontOfSize(14)
        label.backgroundColor = UIColor.cyanColor()
        self.view.addSubview(label)

        let textTF:UITextField = UITextField.init()
        textTF.frame = CGRectMake(140, 80, 200, 30)
        textTF.borderStyle = UITextBorderStyle.RoundedRect
        textTF.placeholder = "輸入姓名"
        self.view.addSubview(textTF)

        let btn:UIButton = UIButton(type: UIButtonType.Custom)
        btn.frame = CGRectMake(20, 120, 200, 30)
        btn.addTarget(self, action: "btnClicked", forControlEvents: UIControlEvents.TouchUpInside)
        btn.backgroundColor = UIColor.blueColor()
        btn.setTitle("提交", forState: UIControlState.Normal)
        self.view.addSubview(btn)
    }
    func btnClicked() {
//        let loginVC:MyTableViewController = MyTableViewController()
//        self.navigationController?.pushViewController(loginVC, animated: true)

//跳轉頁面的同時實現代理
        let loginV:LoginViewController = LoginViewController()
        //代理
        loginV.delegate = self
        //block
        loginV.myBlock = {a in
            print(a)
        }
        self.navigationController?.pushViewController(loginV, animated: true)
    }
    //實現代理方法
    func test() {
        print("我是代理傳值")
    }
    func test2(name: String) {
        print(name)
    }
    func test3(name: String, age: Int) {
        print(name,age)
    }
    func test4(name: String, age: Int) -> (String, Int) {
        print(name,age)
        return (name,age)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}