Course Notes: Cocoa with Swift 3 Essential Training

这是 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

  1. Create a new Xcode project > Language: Swift > 所有选项不选 > Next
  2. 打开 MainMenu.xib > Utilities 面板 > Object Library > Filter: label > 拖入视图 > 双击修改 Label 文本为 Hello world

3. 自定义 Controller 类

  1. file > new > Cocoa Class > 创建 NSObject 的子类 CustomController
  2. 给 MainMenu.xib 添加 Push Button
  3. 按住 Option 键点击 CustomController.swift
  4. Object Library 中拖 Object 到左侧 Delegate Object 下,在 Identity Inspector 面板中设置这个 Object 的 class 为 CustomController
  5. 这时候 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

  1. .xib 中,从 Object Library 拖一个 Submenu Menu Item 至菜单视图
  2. 拖 Menu Item 作为下拉菜单选项
  3. ctrl + 拖 Menu Item 至 .swift 文件创建 Actiion
  4. 或者在 .swift 中写好 @IBAction,然后拖 Menu Item 至 First Responde,在浮动菜单中选择想要执行的 Action,当这个 Menu Item 被点击时,应用就从堆栈中找到实现了这个 Action 的类,执行它。

8. 单选按钮

  1. 拖 3 个 Radio Button 到视图中
  2. 3 个 Button 都指向同一个 @IBAction,它们就形成一组,点击任意一个会变成唯一选中状态
  3. 用 identifer 属性区分每个 Button

9. Formatter

  1. 拖 Label 到视图
  2. 再拖 Date Formatter 或者 Number Formatter 到 Label 上,属性面板中修改需要的格式
  3. 给 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. 视图组织

  1. 拖 3 个 Text Field 到视图
  2. 选中这 3 个 Text Field > Editor > Embed In > Box,这 3 个 Text Field 就放在了一个 box view 中

12. Auto Layout

  1. 右键选择 Leading Space to Container 之类...
  2. 或者点击 pin 后 Add Constraints

13. TableView

  1. 遵循 NSTableViewDataSource
  2. 实现 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

  1. 选中 Label,选择 Binding Inspector 面板
  2. Bind to: Delegate
  3. 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

你可能感兴趣的:(Course Notes: Cocoa with Swift 3 Essential Training)