func setColor(colorType: String) {
if colorType == "red"{
view.backgroundColor = .red
}else if colorType == "green"{
view.backgroundColor = .green
}else if colorType == "blue"{
view.backgroundColor = .blue
}
}
class ViewController: UIViewController,LightRedViewControllerDelegate
實做setColor方法,判斷傳進來的字串後改變背景顏色
func setColor(colorType: String) {
if colorType == "red"{
view.backgroundColor = .red
}else if colorType == "green"{
view.backgroundColor = .green
}else if colorType == "blue"{
view.backgroundColor = .blue
}
}
設置警告控制器:
if inputText == ""{
let myAlert = UIAlertController(title: "No input", message: "Please enter something", preferredStyle: .alert)
//設置警告控制器的按鈕
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
//將按鈕加入警告控制器
myAlert.addAction(okAction)
//推出警告控制器
present(myAlert, animated: true, completion: nil)
}
傳送使用者輸入的資料至第二個畫面:
else{
//change view(由下至上轉場至LightRed)
//sender:傳送使用者輸入的資料
performSegue(withIdentifier: "gotoview2", sender: inputText)
//clear text
myTextInput.text = ""
//收合鍵盤
myTextInput.resignFirstResponder()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//檢查segue的識別碼
if segue.identifier == "gotoview2"{
//將segue的目的地轉型成LightRedViewController
if let lightRed = segue.destination as? LightRedViewController{
//透過lightRed設定inforFromViewOne屬性
//將sender(inputText)設定給上述屬性,傳入第二個畫面
lightRed.inforFromViewOne = sender as? String
//將目的地設為自己
lightRed.delegate = self
}
}
}