002--swift初体验

来源:互联网 发布:电子挂历制作软件 编辑:程序博客网 时间:2024/06/02 13:13

语法:

这里写图片描述

//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.    }}
原创粉丝点击