預(yù)覽.png
"Hello,World"已經(jīng)成為編程世界里開始的慣例倡勇。
開始:先動起來,忘記代碼
- 打開Xcode村生,選擇"Create a new Xcode project"
Xcode - Welcome Dialog.png
- 選擇 iOS > Single View Application and click Next
Xcode Project Template Selection.png
- 填寫項目的一些選項:
Options for your new project.png
- Product Name: HelloWorld - app的名字
- Team - Apple ID
- Organization Name: andyron - 這一般是組織的名字惊暴,或個人用戶的名字;盡量保持不與別人重復(fù)
- Organization Identifier: cn.andyron
- Bundle Identifier: com.andyron.HelloWorld - 根據(jù)Organization Identifier和Product Name自動組合而成
- Language: Swift
- Devices: iPhone
- Use Core Data: [unchecked]
- Include Unit Tests: [unchecked]
- Include UI Tests: [unchecked]
- 選擇一個項目保持位置趁桃,然后生成項目
熟悉Xcode界面
Xcode Help中詳細(xì)介紹了Xcode辽话,主界面如下:
Xcode主界面
主要分六個區(qū)域:
- Toolbar(工具欄)
- Tab bar(便簽頁欄):類似瀏覽器的標(biāo)簽頁,快捷鍵也和大部分軟件的相同(?+T新建卫病,?+W關(guān)閉)油啤,單個標(biāo)簽頁時是隱藏的
- Navigator area(導(dǎo)航區(qū)域)
這個區(qū)域最上面有八個圖標(biāo)(快捷鍵分別對應(yīng)?+1 ~ ?+8):- project navigator(源代碼文件、圖片文件蟀苛、數(shù)據(jù)模型益咬、屬性列表等)
- symbol navigator(編譯器能識別的:OC類、枚舉類型屹逛、結(jié)構(gòu)體础废、全局變量)
- find navigator
- issues navigator
- test navigator
- debug navigator
- breakpoint navigator
- log navigator
-
Editor area(編輯區(qū))
這個區(qū)域最上面是jump bar(跳轉(zhuǎn)欄)
- Debug area
-
Utilities area(實用工具面板)
分上下兩塊:
- Inspector pane(檢查器):選擇不同文件會有不同個數(shù)的檢查器,最多六個(快捷鍵分別對應(yīng)?+option+1 ~ ?+option+6)罕模。
- Library pane(庫面版, 快捷鍵分別對應(yīng)?+option+control+1 ~ ?+option+control+4)
- 文件庫模板(File Template Library) : 管理文件模板, 可以快速創(chuàng)建指定類型文件, 可以直接拖入項目中;
- 代碼片段庫(Code Snippet Library) : 管理各種代碼片段, 可以直接拖入源代碼中;
- 對象庫(Object Library) : 界面組件, 可以直接拖入 故事板中;
- 媒體庫(Media Library) : 管理各種 圖片, 音頻 等多媒體資源;
第一次運行App
Run和Stop分別對應(yīng) ?+R和?+.评腺,Simulator用于選擇模擬器或真實設(shè)備
Run and Stop Buttons
快速了解Interface Builder(IB)
在project navigator中選擇main.storyboard
The Interface Builder Editor.png
The Interface Builder editor為創(chuàng)造和設(shè)計app的UI提高了一個可視化的方法。在這里既可以設(shè)計單個頁面淑掌,也可以設(shè)計多個頁面蒿讥。可以直接從右下角Object library拖動可用對象到IB中抛腕。
當(dāng)開始新建項目芋绸,選擇了Single View Application模板時,Xcode默認(rèn)在storyboard中生成一個view controller scene担敌,對應(yīng)一個view controller摔敛。通常app的每一個screen代表一個view controller。
The Document Outline view在選擇storyboard特定對象時特別有用全封。
設(shè)計UI
- 拖動一個Button到IB中
Drag the Button to the View.png - 雙擊Button或在屬性檢查器中修改name為"Hello World**
- 根據(jù)藍(lán)色虛線輔助線可以把Button拖到中心位置
寫代碼
- 選擇main.storyboard马昙,點擊工具欄右側(cè)的Assistant editor(option+?+return),編輯區(qū)出現(xiàn)兩部分刹悴,Ctrl拖動按鈕到右側(cè)
ViewController
類中:
然后寫下如下代碼:
@IBAction func showMessage() {
let alertController = UIAlertController(title: "Welcome to My First App",
message: "Hello World", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK", style:
UIAlertActionStyle.default, handler: nil))
present(alertController, animated: true, completion: nil)
}
上面的方法是先連接關(guān)系行楞,然后寫代碼;也可以先寫好代碼土匀,然后建立關(guān)系:
@IBAction
表示一個Action的標(biāo)識符子房,沒有特殊意義,當(dāng)建立好關(guān)系后就轧,@IBAction
右側(cè)會出現(xiàn)一個實心的點证杭,點擊會出現(xiàn)會出現(xiàn)以個彈框表示連接的地方:-
修改按鈕的顏色。選中按鈕钓丰,在屬性檢查器中修改對應(yīng)屬性
測試應(yīng)用
選擇特定的模擬器或設(shè)備躯砰,?+R
Hello world 應(yīng)用中事件流
Event flow.png
更近一步
- 同上的方法添加幾個按鈕,并修改button的name為一些emoji表情(control+?+space)
- 添加
@IBAction
方法代碼 - 通過先寫好代碼然后建立關(guān)系的方法携丁,把幾個button都與同一個
@IBAction
方法建立關(guān)系
代碼
說明
此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄