适宜有一定ios开发基础人群
1.打开xcode
2.点击导航栏的file----new----project
3.选择single View App
4.填入Product Name,在language那一栏选择Swift,下一步
5.此时你的项目中有两个后缀为.Swift的文件(Appdelegate ViewController)
6.点击Viewcontroller.swift文件,在Viewdidload接口中创建你的按钮
直接上代码
let newButton:UIButton = UIButton(frame: CGRect(x: 0, y: 20, width: 50, height: 50))
newButton.backgroundColor = UIColor.blue
newButton.setTitle("点我", for: .normal)
newButton.addTarget(self, action: #selector(newButtonAction), for: .touchUpInside)
self.view.addSubview(newButton)
点击事件
@objc func newButtonAction() {
print("select new button")
}
本文介绍如何使用Swift在iOS应用中创建一个按钮,并实现点击事件响应。步骤包括:通过Xcode新建Single View App项目;在ViewController中添加按钮并设置样式;绑定点击事件处理函数。
684

被折叠的 条评论
为什么被折叠?



