您的位置:首页 > 移动开发 > Swift

Swift-传值

2016-06-25 00:12 351 查看
源代码下载地址: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)

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Swift-传值