於 “ViewController.swift” 加入以下
@IBAction func showMessage(sender: UIButton){ let alertController = UIAlertController(title:"My first app", message:"Testing", preferredStyle: UIAlertController.Style.alert) alertController.addAction(UIAlertAction(title:"OK",style:UIAlertAction.Style.default, handler: nil)) present(alertController, animated: true,completion: nil) }
於 “Main.storyboard” ,點擊 你的button, 按住 “control” 後拖動到 “ViewController”, 然後再選擇function名稱(這裡的範例是 “showMessage”) 如下圖