---
# System prepended metadata

title: 12/17 課程內容

---

# 12/17 課程內容

## 大綱
* **Protocol**
* **UIPickerView**
* **Segue**
* **Navigation**



---

##  Protocol
```swift
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource
```

#### `UIViewController` : 
繼承 UIViewController

#### `UIPickerViewDelegate,UIPickerViewDataSource` : 
支援 UIPickerViewDataSource與 UIPickerViewDelegate等 Protocol


---


在 Class新增完 Protocol後，也要將該兩項指派給 ViewController

1. 直接在 Storyboard拉選新增
![](https://i.imgur.com/YVg9RzW.png)
![](https://i.imgur.com/PTEGWR9.png)


2. 在 ViewDidLoad下以程式碼新增 
    ```swift
    pickerView.delegate = self
    pickerView.dataSource = self
    ```



---
## 選擇器 UIPickerView

### UIPickerViewDataSource
首先會有兩個必須實作的 Function : 
* Component數量
    ```swift
    func numberOfComponents(in pickerView: UIPickerView) -> Int 
    ```
* 各Component項目數
    ```swift
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    ```
    
### UIPickerViewDelegate
以及常用的 method : 
* 各項目顯示的文字
    ```swift
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    ```
* 選擇的項目有變動事件時處理的 method
    ```swift
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    ```
    


---

# 段落作業
![](https://i.imgur.com/h7s5tsX.png)


