swift app : 做点小事儿
发布时间:2020-12-14 06:25:27 所属栏目:百科 来源:网络整理
导读:基于之前提到的脚手架,我们再次创建一个swift app,这次做点小东西: 界面包括一个按钮和一个标签,标签初始值为0 当点击按钮时,标签的数字会被累加1 代码如下: import UIKit @UIApplicationMain class AppDelegate: UIResponder,UIApplicationDelegate {
|
基于之前提到的脚手架,我们再次创建一个swift app,这次做点小东西:
代码如下: import UIKit
@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let page = Page1()
self.window!.rootViewController = page
self.window?.makeKeyAndVisible()
return true
}
}
class Page1: UIViewController {
var count = 0
var label : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
label = UILabel()
label.frame = CGRect(x: 100,y: 100,width: 20,height: 50)
label.text = "0"
view.addSubview(label)
let button = UIButton(type: .system)
button.frame = CGRect(x: 120,height: 50)
button.setTitle("+",for: .normal)
button.addTarget(self,action: #selector(Page1.buttonAction(_:)),for: .touchUpInside)
view.addSubview(button)
}
func buttonAction(_ sender:UIButton!){
self.count += 1
label.text = self.count.description
}
}
编译运行后会看到界面上的按钮和标签,点击按钮标签的值加1,说明App满足我们的最初需求。 代码解释下:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
