1. 程式人生 > >Swift之1

Swift之1

1. 宣告:

本部落格僅僅用於學習,水平不足的地方歡迎各位指導。


2. 環境:

 MAC : 10.10.1

Xcode:6.1.1

3. 建立新專案

開啟Xcode,出現如下畫面


在上圖中選擇Create a new Xcode project,進入下面的圖片:


選擇Single View Application, 選擇下一步


在上面的介面中,輸入專案名稱,注意選擇語言為:Swift,選擇Devices位iPhone,然後點選Next,進入下一步


注意把Source Control的checkbox勾去掉,然後點選Create,


在Main.storyboard中點選介面中上部第一個圖示,在右側的屬性中選擇iphone4,這是為了介面小一些,好看些。然後找到ViewControler.swift,在裡面增加滑鼠點選響應函式:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func startGame(){
        let myalert = UIAlertView()
        myalert.title = "準備好了嗎"
        myalert.message = "準備好開始了嗎?"
        myalert.addButtonWithTitle("Ready, go!")
        myalert.show()
    }
}

然後回到Main.storyboard,在右下方的功能中拖一個button到面板上,並在右側修改文字為“開始遊戲”,


右鍵點選按鈕,在彈出的黑色選單中找到touch up inside,拖拽右邊的+號到面板上,然後選擇剛才新增的響應函式,建立事件響應關係。


然後執行程式碼,點選按鈕,可以看到如下的彈出框: