国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

SwiftUI開發(fā)初探(轉(zhuǎn))

2019-11-14 20:25:57
字體:
供稿:網(wǎng)友

原文地址:http://www.tairan.com/archives/6600

關(guān)于Swift語法,可以參考《Apple Swift編程語言入門教程

效果如下:

ui

開發(fā)環(huán)境

  • Xcode6-beta
  • iOS8

創(chuàng)建工程

  • Choose File > New > PRoject > (iOS or OS X) > application > your template of choice.

    此處選擇 Single view Application。

  • Click the Language pop-up menu and choose Swift.
    new-project

添加基本控件

在ViewController.swift文件中進(jìn)行編碼,該文件類似Objective-C的ViewController.m。

  • UILabel

UILabel 控件常用于顯示文本標(biāo)簽

下面我們來創(chuàng)建一個label, 查看UILabel類發(fā)現(xiàn)其繼承于UIView, NSCoding。

可以通過類似創(chuàng)建view的方法,設(shè)置大小和lebel的text,通過addSubview方法將其加到當(dāng)前view上。

代碼如下:

let label =UILabel(frame:CGRect(origin:CGPointMake(10.0,50.0), size:CGSizeMake(150,50)))//let 是Swift 表示常量的關(guān)鍵字        label.text ="This is a Label"self.view.addSubview(label)

 

UILabel創(chuàng)建參數(shù)使用了別名,這點(diǎn)像Object-C。

  • UIButton

UIButton 控件常用于按鈕。

下面我們來創(chuàng)建一個button按鈕,并設(shè)置它的UIControlEvents.TouchUpInside事件的處理,查看UIButton類發(fā)現(xiàn)其繼承于UIControl, NSCoding。

可以通過類似創(chuàng)建view的方法,指定位置和大小創(chuàng)建一個按鈕,然后設(shè)置按鈕的titile,設(shè)置按鈕的背景色,并設(shè)置按鈕的touch事件。

最后通過addSubview方法將其加到當(dāng)前view上。

代碼如下:

let btn =UIButton(frame:CGRect(origin:CGPointMake(10.0,110.0), size:CGSizeMake(150,50)))        btn.setTitle("button", forState:UIControlState.Normal)        btn.backgroundColor =UIColor.redColor()        btn.addTarget(self, action:"buttonClick:", forControlEvents:UIControlEvents.TouchUpInside)self.view.addSubview(btn)

 

buttonClick方法實(shí)現(xiàn)如下:

    func buttonClick(sender:UIButton!){}

 

UIButton后面的! 意味著,sender可以是由UIButton繼承來的任意子類。

  • UIAlertView

UIAlertView 常用于彈出對話框,下面我們來創(chuàng)建一個alert。

UIAlertView類繼承于UIView,我們先創(chuàng)建了一個alert,然后設(shè)置alert的title、message、button、delegate。

然后調(diào)用UIAlertView的show方法,顯示alert。

我們是在button的touch回調(diào)事件中處理alert的創(chuàng)建和顯示的。在buttonClick方法中添加如下代碼:

var alert =UIAlertView()//直接這樣創(chuàng)建有bug//var alert = UIAlertView(title: "alert", message: "this is an alert", delegate: self, cancelButtonTitle: "cancel")        alert.title ="alert"        alert.delegate=self        alert.addButtonWithTitle("cancel")        alert.message ="this is an alert"                alert.show()

 

delegate和self,依然有Object-C的影子。

修改ViewController的聲明,加入U(xiǎn)IAlertViewDelegate

classViewController:UIViewController,UIAlertViewDelegate

 

實(shí)現(xiàn)alert的delegate方法,處理button的click事件。

//處理alert 的button click    func alertView(alertView:UIAlertView!, clickedButtonAtIndex buttonIndex:Int){        println("buttonIndex:/(buttonIndex)")}

 

總結(jié)

Swift 的UIKit API接口和 Objective-C的API接口總體上保持一致,熟悉原來的UIKit接口的話,上手Swift UI開發(fā)應(yīng)該很快。

可以通過文檔和API手冊查看各Objective-C的API 如何使用Swift 的API進(jìn)行編程。

你可以在這里獲取本文的Demo。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 华安县| 吉木乃县| 杭锦后旗| 丹巴县| 堆龙德庆县| 武宣县| 桑日县| 旬邑县| 广饶县| 涞水县| 贵南县| 台东市| 勃利县| 仁布县| 离岛区| 伊春市| 自治县| 河北省| 连南| 威宁| 区。| 宁城县| 丰县| 阿尔山市| 商丘市| 封丘县| 长葛市| 新昌县| 霍城县| 垫江县| 东宁县| 武隆县| 中宁县| 镶黄旗| 浮梁县| 桐梓县| 和田市| 屯昌县| 洛宁县| 红安县| 徐州市|