语法:
//1、@UIApplicationMain就是程序的入口
//2、只有.swift 没有.h/.m 在swift中默认全局共享
//3、所有的代码都包装在{},默认方法都有一个缩进
代码展示:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 1、创建视图
let v = UIView(frame: CGRect(x: 0, y: 20, width: 100, height: 100))
v.backgroundColor = UIColor.red
//添加到当前视图
view.addSubview(v)
self.view.addSubview(v)
// 2、创建一个按钮
let btn = UIButton(type: .contactAdd)
v.addSubview(btn)
btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
}
func clickMe(btn:UIButton) -> () {
print(#function)
print("按钮点击了")
print(btn)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
代码作用详解:
====》快速的标记代码,方便以后进行更改或者跳转到这里