swift第四天学习
来源:互联网 发布:java反射得到属性 编辑:程序博客网 时间:2024/06/07 00:16
欢迎大家评论,问问题,质疑
ViewController.swift
import UIKit
//创建的入口和写UI时的入口是一样的,只不过将语言设置成swift
//遵守协议
class ViewController: UIViewController,SecondViewControllerDelegate {
override func viewDidLoad() { super.viewDidLoad() let button : UIButton = UIButton(type: UIButtonType.System) button.frame = CGRectMake(100, 200, 50, 50) button.backgroundColor = UIColor.magentaColor() button.setTitle("点击", forState: UIControlState.Normal) button.addTarget(self, action: "action", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button)}func action(){let secondVC = SecondViewController() secondVC.delegate = self secondVC.sendMessageBlock = { (a : Int) in print("block传过来的值\(a)") } self.presentViewController(secondVC, animated: true, completion: nil)}func getNumber(number: Int) { print(number)}
}
SecondViewController.swift
import UIKit
//定义一个不是必须实现的协议(必须有 @objc 和 optional )
@objc protocol SecondViewControllerDelegate {
optional func getNumber(number : Int)
}
//类型命名
typealias block = ((Int)->Void)
class SecondViewController: UIViewController {
//定义一个delegate,类型为SecondViewControllerDelegate
var delegate : SecondViewControllerDelegate?
//定义一个block属性
var sendMessageBlock : block! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.cyanColor()
//初始化一个Button
let button : UIButton = UIButton(type:UIButtonType.System)
button.frame = CGRectMake(100, 200, 50, 50)
button.backgroundColor = UIColor.redColor()
button.setTitle(“点击”, forState: UIControlState.Normal)
button.addTarget(self, action: “action”, forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func action(){
//判断?修饰的是否有值
if let a = self.delegate {
print(“111(a)”)
a.getNumber!(1)
}
self.sendMessageBlock(2)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
- swift第四天学习
- swift学习第四天
- swift学习第四章
- Swift学习第四枪
- Swift学习笔记第四篇(函数)
- swift學習第四天 swift與OC的交互
- java学习第四天
- COBOL学习第四天
- ajax学习第四天
- 学习Java第四天
- 程序学习第四天
- 学习jsp第四天
- Java学习第四天
- C#学习第四天
- 【学习STM32F4】第四天
- mahout学习第四天
- Python 学习第四天
- Qt学习第四天
- leetcode -- Sort List
- Linux下网络IP地址的转换函数
- 解决 android 高低版本 webView 里内容 自适应屏幕的终极方法
- leetcode Majority Element
- android 关于paint的设置
- swift第四天学习
- STL算法库-非修改序列式操作(一)
- Tomcat部署Web应用
- SAXParseException; lineNumber: 21; columnNumber: 7; 元素类型为 "set" 的内容必须匹配 "(met
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- Leetcode190: Game of Life
- 利用栈的原理计算一个表达式的值
- leetcode Excel Sheet Column Title
- 这两天都忘了写了,养成一个习惯真不容易