Swift-传值

源代码下载地址:http://download.csdn.net/detail/u012450066/9559017
建议下载源代码,更容易理解一点。

1. 属性传值

值是从第一个界面传给第二个界面

第一个界面

    /**
     属性传值

     - parameter indexPath: indexPath
     */
    func propertyTransferValue(indexPath: NSIndexPath) {
        let vc = SecondViewController()
        vc.value = datas[indexPath.row]
        vc.someArrays = datas
        navigationController?.pushViewController(vc, animated: true)
    }

第二个界面,接收值

class SecondViewController: UIViewController {

    var someArrays = [String]()

    var value : String?

    @IBOutlet weak var transferLabelValue: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = value
        transferLabelValue.text = value
        print(someArrays)
    }

}

2.代理传值

值是从第二个界面传回第一个界面

第一个界面

    /**
     代理传值

     - parameter indexPath: indexPath
     */
    func protocolTransferValue(indexPath: NSIndexPath) {
        let vc = ProtocolViewController()
        vc.value = datas[indexPath.row]
        vc.delegate = self;
        navigationController?.pushViewController(vc, animated: true)
    }

    /**
     ProtocolViewControllerDelegate

    要遵守ProtocolViewControllerDelegate协议, 以“ , ”分开,没有OC中的<>

    class FirstViewController: UIViewController, UITableViewDelegate,     UITableViewDataSource, ProtocolViewControllerDelegate{}
     - parameter value: 代理传回来的值
     */
    func protocolTransfer(value: String) {
        datas[index.row] = value
        tableView.reloadData()
    }

第二个界面

protocol ProtocolViewControllerDelegate {
    func protocolTransfer(value: String)
}

class ProtocolViewController: UIViewController {

    @IBOutlet weak var protocolLabel: UILabel!
    @IBOutlet weak var protocolButton: UIButton!

    var value : String?
    //   =nil
    var delegate : ProtocolViewControllerDelegate? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = value

    }

    @IBAction func protocolButtonAction(sender: UIButton) {
        delegate?.protocolTransfer(protocolLabel.text!)
        navigationController?.popViewControllerAnimated(true)
    }
}

3.闭包传值(oc中的代码块)

值是从第二个界面传回第一个界面

第一个界面

    /**
     闭包

     - parameter indexPath: indexPath
     */
    func closureTransferValue(indexPath: NSIndexPath) {
        let vc = ClosureViewController()
        vc.clousureVoid = { () -> Void in
            print("回调过来了")
        }

    vc.clousureValue = { (text : String) -> Void in
            print("回调传回来的值 = \(text)");
        }
        navigationController?.showViewController(vc, sender: nil)
    }

第二个界面

import UIKit

typealias clousureVoidType = () -> Void
typealias clousureValueType   = (text : String) -> Void

class ClosureViewController: UIViewController {

    var clousureVoid : clousureVoidType?
    var clousureValue : clousureValueType?


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        /** 只是一个回调 */
        clousureVoid!();

        let btn = UIButton.init(type:.Custom)
        btn.frame = CGRectMake(0, 0,100, 50)
        btn.center = view.center;
        btn.backgroundColor = UIColor.cyanColor()
        btn.setTitle("闭包传值", forState: .Normal)
        btn.addTarget(self, action: #selector(ClosureViewController.btnAction), forControlEvents: .TouchUpInside)
        view.addSubview(btn)
    }

   func btnAction() {
        /** 传值 */
        clousureValue!(text: "ClosureViewController")
        navigationController?.popViewControllerAnimated(true)
    }

}

4.通知传值

值是从第二个界面传回第一个界面

第一个界面

 // MARK: - 添加通知
    func addNotifacetion () {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FirstViewController.notifacetionTransferAction(_:)), name: "notifacetionTransfer", object: nil)
    }

    /** 接收到通知后执行方法 */
    func notifacetionTransferAction(notifacetion : NSNotification) {
        print("\(notifacetion.userInfo)")
    }

    // MARK: - deinit
    /** 移除通知 */
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

第二个界面

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        NSNotificationCenter.defaultCenter().postNotificationName("notifacetionTransfer", object: self, userInfo: ["key":"value"])
        navigationController?.popViewControllerAnimated(true)

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值