# Finish add item

## เอาค่าที่กรอกมาเพิ่มสิ

ตอนนี้เราเพิ่ม todo item ได้ละ แต่เพิ่มเป็น Test หมดเลย ต่อไปเราจะใช้ค่าที่ผู้ใช้กรอกเข้ามาจริง ๆ ละ

เริ่มจากการผูก Outlet เพื่ออ่านค่า property ของ view ทั้งสองอันคือ TextField และ Switch

วิธีการแบบเดียวกันกับตอนที่เราผูก Action เลยเพียงแต่ตอนนี้ให้เลือกเป็น Outlet และตั้งชื่อว่า **titleTextField** กับ **isDoneSwitch**

![](/files/-LfKYr3RNqEeE64SdSV8)

จะได้โค้ดแบบนี้เพิ่มมา

```swift
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var isDoneSwitch: UISwitch!
```

จากนั้นเราก็แก้ **doneButtonDidTap(\_:)** ให้อ่านค่าจาก Outlet ทั้งสองตัวเป็นอันจบ

```swift
@IBAction func doneButtonDidTap(_ sender: UIBarButtonItem) {
    if let title = titleTextField.text, !title.isEmpty {
        let todoItem = TodoItem(title: title, isDone: isDoneSwitch.isOn)
        delegate?.addNewItemViewController(controller: self, didAdd: todoItem)
    }
}
```

จากโค้ดจะเห็นว่า เรามี check empty ด้วย ถ้า TextField ยังว่างอยู่จะไม่ add ให้

![](/files/-LfKZgx80oUxrv6eGvso)

## Adjust UX

เพื่อให้ผู้ใช้สะดวกขึ้น เรามาให้โฟกัสเปิดแป้นพิมพ์อัตโนมัติหลังจากเข้าหน้า ผู้ใช้จะได้ไม่ต้องมาแตะที่ช่องอีกรอบ โดยการเพิ่ม **titleTextField.becomeFirstResponder() หลังจากที่หน้าจอแสดงเสร็จแล้ว**

```swift
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    titleTextField.becomeFirstResponder()
}
```

โค้ดสุดท้ายหลังจบบทนี้

{% code title="AddNewItemViewController.swift" %}

```swift
import UIKit

protocol AddNewItemViewControllerDelegate: class {
    func addNewItemViewController(controller: AddNewItemViewController, didAdd item: TodoItem)
    func addNewItemViewControllerDidCancel(controller: AddNewItemViewController)
}

class AddNewItemViewController: UIViewController {

    weak var delegate: AddNewItemViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBOutlet weak var titleTextField: UITextField!
    @IBOutlet weak var isDoneSwitch: UISwitch!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        titleTextField.becomeFirstResponder()
    }

    @IBAction func doneButtonDidTap(_ sender: UIBarButtonItem) {
        if let title = titleTextField.text, !title.isEmpty {
            let todoItem = TodoItem(title: title, isDone: isDoneSwitch.isOn)
            delegate?.addNewItemViewController(controller: self, didAdd: todoItem)
        }
    }

    @IBAction func cancelButtonDidTap(_ sender: UIBarButtonItem) {
        delegate?.addNewItemViewControllerDidCancel(controller: self)
    }
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pakornpat.gitbook.io/ios-app/v2/finish-add-item-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
