swift闭包传值 简单的传值
class ViewController: UIViewController {
//声明一个控制器
let viControl = ggViewController()
override func viewDidLoad() {
super.viewDidLoad()
// let intestt = increment(amount: 30)
// let b = intestt()
// let c = intestt()
// let d = intestt()
// print(b)
// print(c)
/// print(d)
// let nmeArray:Array = ["d","a","c","f"]
// let name = nmeArray.sorted { (a, b) -> Bool in
// return a>b
// }
// let me = nmeArray.sorted(by: maee(s1: 2, s2: 3))
// print(name);
let but = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 90))
but.backgroundColor = UIColor.red
view.addSubview(but)
but .addTarget(self, action: #selector(ViewController.sf), for: .touchUpInside)
let but1 = UIButton(frame: CGRect(x: 60, y: 90, width: 60, height: 90))
but1.backgroundColor = UIColor.red
view.addSubview(but1)
but1 .addTarget(self, action: #selector(ViewController.sfc), for: .touchUpInside)
// addView { (viewa) in
// let but = UIButton(frame: CGRect(x: 0, y: 0, width: viewa.frame.width/2, height: viewa.frame.height/2))
// but.backgroundColor = UIColor.red
// viewa.addSubview(but)
// but .addTarget(self, action: #selector(ViewController.sf), for: .touchUpInside)
// print("ggggggg")
// print(viewa)
// }
}
//这个测试不管用
func sfc() -> Void {
viControl.backClosure = {
(bak:String) -> Void in
//返回来显示的
print(bak)
}
//跳转页面
self.present(viControl, animated: true, completion: nil)
}
func sf() -> Void {
//这个不管用
viControl.backClosure = {
(bak:String) -> Void in
//
print(bak)
}
print("点击")
}
func addView(addButton:(Int)->Void) -> Void {
var viewa = UIView();
let a = 345
viewa = UIView(frame: CGRect(x: 3, y: 40, width: 100, height: 200))
viewa.backgroundColor = UIColor .yellow
// view.addSubview(viewa)
addButton(a)
}
func dik(mue:Int,mue2:Int,adefucot:(Int,Int)->Int) ->Int{
return adefucot(mue, mue2)
}
func maee(s1:String,s2:String) -> Bool {
return s1>s2
}
func add(ade:Int) ->(Int) -> Int {
return {
mun in
return mun+ade
}
}
func increment(amount: Int) -> (() -> Int) {
var total = 0
func incrementAmount() -> Int {
// total 是外部函数体内的变量,这里是可以捕获到的
total += amount
return total
}
// 返回的是一个嵌套函数(闭包)
return incrementAmount
}
//界面二
import UIKit
//声明一个闭包 带有参数
typealias InputClosureType = (String) -> Void
class ggViewController: UIViewController {
var backClosure : InputClosureType?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.yellow
// Do any additional setup after loading the view.
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//返回第一界面时 传值
if self.backClosure != nil {
// if let tempString = self.inputTextField.text {
self.backClosure!("dddggg")
// }
}
// backClosure!("dddd")
// self.dismiss(animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
self.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}