🖥️
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

MVC

PreviousBasic Auto LayoutNextModel

Last updated 2 years ago

MVC

MVC เป็นการแบ่งส่วนโปรแกรมออกมาเป็น 3 ส่วนหลัก ๆ นั่นคือ Model, View และ Controller

Model เป็นส่วนที่จะบอกว่าแอปของเราทำอะไร ไม่ใช่บอกว่าแสดงอย่างไร สามารถนำไปใช้ได้โดยไม่ขึ้นว่าหน้าตาของแอปเราจะเป็นอย่างไร What your application is (not how it is displayed)

Controller เป็นส่วนที่ทำหน้าที่ว่าจะเอาโมเดลนั่นมาแสดงให้ผู้ใช้งานอย่างไร โดยอาศัย View อีกทีนึง (How your model is presented to user)

View เป็นเหมือนลูกกระจ๊อกของ Controller ในการใช้แสดงผลและรับ interaction จากผู้ใช้ เช่น Button, TextField, Label หรืออื่น ๆ (Controller's minions)

Controller จะติดต่อโดยตรงกับ Model และ View โดยที่การติดต่อกับ View เราจะเรียกว่า เชื่อมผ่าน Outlet

Model กับ View จะไม่คุยกันเอง เพราะ Model จะต้องไม่ยุ่งเกี่ยวกับ View

แล้ว View จะคุยกับ Controller ได้ไหม คำตอบคือคุยแบบไม่รู้ว่า Controller นั้นเป็นใคร View จะไม่รู้จัก Controller นั้นโดยตรง ถ้าเป็น Action เช่นกดปุ่ม Controller จะทำหน้าที่ตั้ง Target ไว้บนตัวเองแล้วให้ View ยิง Action มาหา หรือ View เรียกผ่าน Delegate, DataSource โดย controller จะทำตัวเองเป็น Delegate หรือ DataSource แล้วบอก view อีกทีหนึ่ง

แล้วโมเดลหล่ะ ถ้ามีข้อมูลเปลี่ยนต้องบอก Controller ให้ update view จะทำอย่างไร คำตอบคือ model สามารถส่งสัญญาณออกมาได้เหมือนตั้งเสาวิทยุ โดยที่ controller จะต้องตั้งเสารับเอาไว้ (นึกถึง model broadcast noti ออกมาโดยมี Controller observe อยู่)

MVC สามารถมีได้หลาย ๆ ชุดเชื่อมโยงต่อกันโดยใช้การสื่อสารตามที่ระบุไปข้างต้น

ดูรายละเอียดเพิ่มเติมได้จากคลาส Stanford CS 193P 2017 Lecture 2: MVC; iOS, Xcode and Swift Demonstration บน บน

Youtube ไม่ใช่ของ Official
Podcasts ของ Official
แบ่งออกเป็นสามส่วนหลัก ๆ