01. showAlert()函數(shù)中追加判斷,根據(jù)差值確定等級(jí),并提示給用戶。
02. 局部變量 local variables 和 實(shí)例變量instance variables。
- local variables 僅存在于它所定義的方法中。每次方法被調(diào)用,它的成員變量和常量都被重新生成。
- instance variables 與它所在的對(duì)象同生共死。一般定義在每個(gè)文件的最上面。
- 上述兩種變量最好在命名的時(shí)候就加以區(qū)分。
03. 將更新label和開始新一輪的處理,修改執(zhí)行位置
- 修改前:彈出pop 就立即執(zhí)行,更新lable
- 修改后:在彈出pop,并按下pop的OK按鈕后,再執(zhí)行
# 修改前
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
# 修改后
let action = UIAlertAction(title: "OK",style: .Default,
handler: { action in
self.startNewRound()
self.updateLabel()
})
上述代碼塊叫做closure,閉包,可以看做是一種沒有名稱的方法,這段代碼并不立即執(zhí)行,只有在OK的這個(gè)action執(zhí)行完后才執(zhí)行。在這段代碼塊后面的代碼繼續(xù)執(zhí)行,比如后面的pop顯示等。
為什么要在閉包中加入self?
This is a rule in Swift. If you forget self in a closure, Xcode doesn’t want to build your app (try it out). This rule exists because closures can “capture” variables, which comes with surprising side effects. You’ll learn more about that in the other tutorials.
04. 添加startOver的處理
startOver函數(shù),用來重啟游戲,比如換一個(gè)人玩的時(shí)候,需要將前一個(gè)人的分?jǐn)?shù)和輪數(shù)重置。
func startover(){
score = 0
roundNumValue = 0
startNewRound()
updateLabel()
}
添加上述函數(shù),然后在添加startOver與一個(gè)action方法的連接,在action方法中調(diào)用上述函數(shù)。
05. 添加about
- 添加cocoa Touch class文件,設(shè)置父類為UIViewController。
- 選擇Main.storyboard,添加一個(gè)view controller,并在AttributeInspector中將Orientation設(shè)置為Landscape橫屏。
- 添加button和textview控件。
- 添加主界面中i 按鈕和 aboutviewController之間的聯(lián)系。按住Ctrl拖動(dòng)到about窗口,選擇modal完成一個(gè)界面的遷移(segues)。
- segues也是有屬性的,選擇segues的Attribute Inspector,將Transition設(shè)置為FlipHorizontal,設(shè)置界面遷移時(shí)的動(dòng)畫。
- 設(shè)置新添加的viewcontroller的類為 AboutViewController。
- 為AboutViewController的close按鈕添加方法。
@IBAction func close(sender: UIButton) {
dismissViewControllerAnimated(true, completion: nil)
}
TODO
- Xcode上各個(gè)操作按鈕的意義。