這是 Lynda - Cocoa with Swift 3 Essential Training 的學習筆記。由 Todd Perkins 老師講授。
1. Cocoa 解釋
基于 Mac 平臺開發的 App 都是 Cocoa App,基于 iOS 開發的 App 的技術支持是 Cocoa Touch。實際上 Cocoa 是 Apple 用來表述所有基于 Mac/iOS 系統的開發技術。更多信息在 Mac Technology Overview。
2. Hello World
- Create a new Xcode project > Language: Swift > 所有選項不選 > Next
- 打開 MainMenu.xib > Utilities 面板 > Object Library > Filter: label > 拖入視圖 > 雙擊修改 Label 文本為 Hello world
3. 自定義 Controller 類
- file > new > Cocoa Class > 創建 NSObject 的子類 CustomController
- 給 MainMenu.xib 添加 Push Button
- 按住 Option 鍵點擊 CustomController.swift
- Object Library 中拖 Object 到左側 Delegate Object 下,在 Identity Inspector 面板中設置這個 Object 的 class 為 CustomController
- 這時候 MainMenu.xib 中的 Button 才可以做這個關聯操作:ctrl + 左鍵拖放至 CustomController.swift,創建一個 Action
4. Alert
let alert = NSAlert()
alert.messageText = "You can't do that!"
alert.runModal()
5. Alert Sheets
let alert = NSAlert()
alert.messageText = "You can't do that!"
alert.addButton(withTitle: "Button 1")
alert.addButton(withTitle: "Button 2")
alert.addButton(withTitle: "Button 3")
alert.beginSheetModal(for: window) { (responsCode: NSModalResponse) in
if(responsCode == NSAlertFirstButtonReturn){
print("Button 1")
}
}
6. 自定義 Delgate
定義一個 protocol
protocol CustomDelegate {
func customDelegateExample()
}
創建 OtherObject.swift 遵循這個 protocol
class OtherObject: NSObject, CustomDelegate {
func customDelegateExample() {
print("delegation is working!")
}
}
在 AppDelegate.swift 中聲明這個對象和delegate
var other:OtherObject!
var myDelegate:CustomDelegate!
實例化 class,告訴 myDelegate 誰實現了這個 protocol,執行委托方法
other = OtherObject()
myDelegate = other
myDelegate.customDelegateExample()
7. Menu
- .xib 中,從 Object Library 拖一個 Submenu Menu Item 至菜單視圖
- 拖 Menu Item 作為下拉菜單選項
- ctrl + 拖 Menu Item 至 .swift 文件創建 Actiion
- 或者在 .swift 中寫好 @IBAction,然后拖 Menu Item 至 First Responde,在浮動菜單中選擇想要執行的 Action,當這個 Menu Item 被點擊時,應用就從堆棧中找到實現了這個 Action 的類,執行它。
8. 單選按鈕
- 拖 3 個 Radio Button 到視圖中
- 3 個 Button 都指向同一個 @IBAction,它們就形成一組,點擊任意一個會變成唯一選中狀態
- 用 identifer 屬性區分每個 Button
9. Formatter
- 拖 Label 到視圖
- 再拖 Date Formatter 或者 Number Formatter 到 Label 上,屬性面板中修改需要的格式
- 給 Label 的 objectValue 賦值
let date = NSDate()
dateLabel.objectValue = date
10. API 創建用戶界面
let label = NSTextField(labelWithString: "example label")
label.frame = NSRect(x: 100, y: 100, width: label.frame.width, height: label.frame.height)
window.contentView?.addSubview(label)
11. 視圖組織
- 拖 3 個 Text Field 到視圖
- 選中這 3 個 Text Field > Editor > Embed In > Box,這 3 個 Text Field 就放在了一個 box view 中
12. Auto Layout
- 右鍵選擇 Leading Space to Container 之類...
- 或者點擊 pin 后 Add Constraints
13. TableView
- 遵循 NSTableViewDataSource
- 實現 numberOfRows 和 tableView(_ tableView: NSTableView, objectValueFortableColumn: NSTableColumn?, row: Int) -> Any? 方法
14. key-value
可以這樣設置一個對象的屬性
book.author = "Todd"
也可以這樣設置
book.setValue("Jimmy", forKey: "author")
讀取
print(book.value(forKey: "author"))
15. 綁定對象至 UI
- 選中 Label,選擇 Binding Inspector 面板
- Bind to: Delegate
- Model Key Path: self.book.author
16. NSException
創建一個 NSException
NSException(name: NSExceptionName.illegalSelectorException, reason:"what you just tried to do is not cool", userInfo: nil).raise()
創建一個 Exception Breakpoints 捕獲它
17. Assertion
在不滿足條件時拋出 assert
let num = 101
assert(num == 100, "num is supposed to be 100!")
18. 分發
非 Mac Store 的分發可以選擇 Product > Archive > Export as a macOS App