🖥️
iOS App with Pop
UIKit Swift 5
UIKit Swift 5
  • iOS App development
  • Swift
    • Variable & Constant
    • Number & String
    • Operator
    • Array, Dictionary & Tuple
    • Enum
    • Optional
    • Function
    • Class & Struct
    • Branching
    • Loops
    • Error handler
    • Protocol
    • Extension
  • Create New Project
  • Introduction to Xcode
  • Scene-Based Life-Cycle
  • UIViewController
  • Storyboard
  • First Run
  • Display todo list
  • Basic Auto Layout
  • MVC
  • Model
  • Binding TableView
  • Binding TableViewCell
  • TableViewDelegate
  • Add navigationBar with + button
  • Add new item page
  • TextField and Switch
  • Binding action
  • Add mock item to todo list
  • What is weak?
  • Finish add item
  • Delete todo item
  • Edit todo item
  • Custom new layout
  • Adding new delegate
  • Refactor
  • Pushing edit view
  • Large navigation
  • Drag item
  • Drop item (in app)
  • Save data
  • Where to go from here?
Powered by GitBook
On this page
  • Controller ของหน้าจอ
  • Cycle ของ ViewController

UIViewController

PreviousScene-Based Life-CycleNextStoryboard

Last updated 2 years ago

Controller ของหน้าจอ

โดยปกติหน้าจอแต่ละหน้าจอใน Storyboard จะมีโค้ดที่ใช้ในการควบคุมหน้าจอนั้น เพื่อให้หน้าจอแสดงค่า และรับ action ของผู้ใช้ ส่วนนี้เราจะเรียกว่า ViewController

ตัวอย่างของ ViewController ที่ Xcode สร้างให้ในตอนแรก

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

Cycle ของ ViewController

เมื่อ ViewController นั้นถูกเรียกให้แสดงผล ถ้ายังไม่เคยถูกสร้างมาก่อน เมื่อสร้างเสร็จและเชื่อมโยง View ต่าง ๆ เช่น Label, TextField จะเรียก viewDidLoad เมื่อกำลังจะถูกแสดงขึ้นมาจะเรียก viewWillAppear และเมื่อแสดงเสร็จแล้วจะกลายเป็น viewDidAppear

ในขณะที่จะหายไปก็เช่นกัน จะเรียก viewWillDisappear และเมื่อหายไปแล้วก็จะเป็น viewDidDisappear

สามารถอ่านเพิ่มเติมเกี่ยวกับเรื่องนี้ได้ที่

Handling View-Related Notifications