Alert สำคัญในการพัฒนา iOS ใช้ UIAlertController อ่านรายละเอียด
ตัวอย่าง: กดปุ่ม แสดง Alert พร้อมข้อความ
ขั้นตอนที่ 1: สร้างโปรเจกต์
Xcode → New Project → Single View Application → ชื่อ "Alert"
ขั้นตอนที่ 2: เพิ่มปุ่มและ IBAction
Main.storyboard: เพิ่มปุ่มชื่อ "Tap" @IBAction ชื่อ tap
ขั้นตอนที่ 3: โค้ดแสดง Alert (3 ขั้นตอน)
- สร้าง Alert Object จาก UIAlertController
- เพิ่ม Action ให้ Alert
- Present Alert
@IBAction func tap(_ sender: Any) {
let uialert = UIAlertController(title: "Welcome",
message: "Welcome to my channel. Thanks for watching. Click on Okay to continue",
preferredStyle: .alert)
uialert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
self.present(uialert, animated: true, completion: nil)
}ขั้นตอนที่ 4: รันทดสอบ
