🖥️
iOS App with Pop
Swift 4
Swift 4
  • iOS App development
  • Create New Project
  • Introduction to Xcode
  • App's 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
  • Finish add item
  • Delete todo item
  • Edit todo item
  • Custom new layout
  • Adding new delegate
  • Refactor
  • Save data
  • Pushing edit view
  • Large navigation
  • Drag item
  • Drop item (in app)
  • Where to go from here?
Powered by GitBook
On this page
  • Controller ของหน้าจอ
  • Cycle ของ ViewController

UIViewController

Controller ของหน้าจอ

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

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

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Cycle ของ ViewController

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

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

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

PreviousApp's life cycleNextStoryboard

Last updated 6 years ago