Swift基礎教程(第一課 第一個Swift應用)

來源:互聯網
上載者:User

標籤:ios 8開發   iphone 6   swift   swift開發   戴維營教育   

  1. 從LaunchPad中啟動Xcode6,選擇“create new project”:
    650) this.width=650;" class="alignnone wp-image-548" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/new_project.png" alt="new_project" width="529" height="316" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />


    可以看到,Swift語言的項目與Objective-C的項目基本上是一樣的,除了.m結尾的Objective-C源碼換成了.swift結尾的Swift語言源檔案了。
    650) this.width=650;" class="alignnone wp-image-549" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/source_file.png" alt="source_file" width="346" height="331" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />

2 . Xcode 6在Storyboard裡為iPad和iPhone增加了統一的介面管理功能,和AutoLayou一起工作。為了簡單起見,先禁用AutoLayout。
650) this.width=650;" class="alignnone wp-image-550" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/size_class.png" alt="size_class" width="320" height="214" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />

禁用AutoLayout時會彈出對話方塊選擇是iPhone還是iPad,我們選擇開發iPhone應用。
650) this.width=650;" class="alignnone wp-image-551" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/layout.png" alt="layout" width="383" height="213" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />

3 . 從控制項欄中按住滑鼠左鍵拖入一個Label標籤,雙擊控制項添加文字,可以在右側的屬性欄修改文字顏色、字型、對齊等:
650) this.width=650;" class="alignnone wp-image-552" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/label.png" alt="label" width="439" height="388" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />

4 . 點擊Xcode左上方的運行按鈕,構建並運行程式。
650) this.width=650;" class="alignnone wp-image-553" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/run.png" alt="run" width="537" height="656" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />
運行按鈕旁邊是停止按鈕,可以關閉應用程式。

5 . 為了在代碼中能夠擷取到這個標籤對象,我們需要在Storyboard和對應的控制器類中進行串連。直接啟動輔助編輯器進行連線是最簡單的。
650) this.width=650;" class="alignnone wp-image-554" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/associate_editor.png" alt="associate_editor" width="610" height="324" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />

6 . Xcode會自動在ViewController中產生IBOutlet修飾的屬性label

////  ViewController.swift//  SwiftApp_1////  Copyright (c) 2014 長沙戴維營教育. All rights reserved.//import UIKitclass ViewController: UIViewController {    //Xcode自動產生的屬性,用@IBOutlet修飾後可以在Storyboard中看到      @IBOutlet var label : UILabel    override func viewDidLoad() {        super.viewDidLoad()        //在代碼中修改label標籤的內容        self.label.text = "戴維營教育歡迎你!"    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

7 . 程式的運行結果如下:
650) this.width=650;" class="alignnone wp-image-555" src="http://blog.diveinedu.net/wp-content/uploads/2014/06/first_result.png" alt="first_result" width="514" height="786" style="margin:5px 20px 20px 0px;padding:0px;border:0px;font-family:inherit;font-size:inherit;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;vertical-align:middle;height:auto;" />


Swift基礎教程(第一課 第一個Swift應用)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.