swift 中的闭包回调
来源:互联网 发布:js改变全局变量的值 编辑:程序博客网 时间:2024/06/06 02:12
需要注意的 地方
1.第一个页面要实现 回调中的需要做的事件
2.第二个页面要声明 闭包
3. 第二个页面在合适的时候调用闭包
4. 注意swift的语法
一、第一个界面
二、第二个界面import UIKit
class ViewController: UIViewController {
let label = UILabel.init(frame: CGRectMake(100, 200, 100, 50))
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.init(type: .Custom)
button.frame = CGRectMake(100, 300, 100, 100)
button.backgroundColor = UIColor.brownColor()
button.setTitle("点我点我", forState: .Normal)
button.addTarget(self, action: #selector(ViewController.nextViewController), forControlEvents: .TouchUpInside)
self.view.addSubview(button)
label.backgroundColor = UIColor.greenColor()
self.view.addSubview(label);
}
func nextViewController() {
let next = MyViewController()
next.block = {
self.label.text = $0
}
self.navigationController?.pushViewController(next, animated: true)
}
}
import Foundation
class MyViewController : UIViewController {
var block : ((showString:String) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
let button = UIButton.init(type: .Custom)
button.frame = CGRectMake(100, 100, 100, 100)
button.setTitle("点我点我", forState: .Normal)
button.addTarget(self, action: #selector(MyViewController.blockAction), forControlEvents: .TouchUpInside)
button.backgroundColor = UIColor.cyanColor()
self.view.addSubview(button)
}
func blockAction() {
self.block?(showString: "hello,world")
self.navigationController?.popViewControllerAnimated(true)
}
}
- swift 中的闭包回调
- [Swift]Swift中的!和?
- [Swift]Swift 中的指针使用
- 【Swift】中的一些关键字
- Swift中的反射
- swift中的?和!
- Swift中的标准函数
- Swift中的循环语句
- Swift中的一些关键字
- Swift中的集合类型
- Swift中的HTTP请求
- Swift中的函数
- swift中的几种传值
- Swift 中的指针使用
- Swift中的!和?
- swift中的数组Array
- swift中的字典Dictionary
- Swift 中的指针使用
- 第八周项目1——(1)数组做数据成员
- Info.plist中常用的key简介
- Docker常用命令
- QT中常规表达式例子
- win7系统端口映射
- swift 中的闭包回调
- Ubuntu Linux内核编译步骤
- LeetCode *** 95. Unique Binary Search Trees II
- mysql ON DUPLICATE KEY UPDATE语句示例
- 下周二推出“音视频技术WebRTC初探”公开课,欢迎捧场!
- [转]Go语言并发之美
- 2015全国硕士研究生计算机考研真题(附答案)
- 【SSH框架】--SpringAOP
- arguments的callee属性