Swift 闭包(Closures)传值
2016-04-18 17:26
429 查看
//
// ViewController.swift
// YlwDome
//
// Created by ylw on 16/2/18.
// Copyright © 2016年 Ylw. All rights reserved.
//
=========================================
//
// ViewControllerTwo.swift
// YlwDome
//
// Created by ylw on 16/2/18.
// Copyright © 2016年 Ylw. All rights reserved.
//
// ViewController.swift
// YlwDome
//
// Created by ylw on 16/2/18.
// Copyright © 2016年 Ylw. All rights reserved.
//
import UIKit class ViewControllerOne: UIViewController { var name:String = "" //MARK: -生命周期 override func viewDidLoad() { super.viewDidLoad() let make = CGRectMake(40, 80, 30, 30) let bt = UIButton(frame: make) bt.backgroundColor = UIColor.blueColor() bt.addTarget(self, action: #selector(ViewControllerOne.click), forControlEvents: .TouchUpInside) self.view.addSubview(bt) } override func viewDidAppear(animated: Bool) { print("\(name)") } deinit { print("ViewControllerOne deinit") } //MARK: -私有方法 func click() { let vcTwo = ViewControllerTwo() vcTwo.backClick = { [unowned self] (str:String) in self.name = str } self.navigationController?.pushViewController(vcTwo, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
=========================================
//
// ViewControllerTwo.swift
// YlwDome
//
// Created by ylw on 16/2/18.
// Copyright © 2016年 Ylw. All rights reserved.
//
import UIKit class ViewControllerTwo: UIViewController { typealias backMethod = (String) -> Void var backClick:backMethod? //MARK: -生命周期 override func viewDidLoad() { super.viewDidLoad() let make = CGRectMake(40, 80, 30, 30) let bt = UIButton(frame: make) bt.backgroundColor = UIColor.redColor() bt.addTarget(self, action: #selector(ViewControllerTwo.Click), forControlEvents: .TouchUpInside) self.view.addSubview(bt) } deinit { print("ViewControllerTwo deinit") } //MARK: -私有方法 //@objc 可加可不加 参考: http://stackoverflow.com/questions/30027322/how-to-call-private-class-function-from-selector-in-swift/34291667#34291667 @objc private func Click() { if backClick != nil { backClick!("ylw") } self.navigationController!.popViewControllerAnimated(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } /* // 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. } */ }
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift实现iOS应用中短信验证码倒计时功能的实例分享
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载
- Swift中的Access Control权限控制介绍