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滑动手势
- Swift中设置tableview的分割线(separator)的样式、颜色、边距
- 自学php,用php服务端和swift客户端实现验证登录和注册功能 1
- 详解Swift中的函数及函数闭包使用
- Swift - 文本输入框(UITextField)的用法
- Swift 析构器deinit
- Swift学习---基础三(区间运算符、控制器转移语句)
- 复习Swift
- swift2.0 学习笔记Nineteen
- 跟着洲洲哥一块学习Swift-数组
- Swift - 按钮(UIButton)的用法
- Swift-UI多选删除
- Swift - 文本标签(UILabel)的用法
- Swift开发 UIColor分类Hex颜色转换
- swift修改webview上下滚动出边界的的背景黑色
- Swift - 协议(protocol)
- Swift - 类扩展(extension)
- swift: 对字典的简易说明
- swift: 关于字符串的使用推荐:
- [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfil