兩個textfield 相加

把兩個textfield裡的值相加

本來是字串相加 我小改變成數字相加

按RETURN和BUTTON會關鍵盤

我這次沒使用delegate

改用事件 Did End on exit

所以一樣拉線事件選Did End on exit

按下return一樣會執行程式內容

程式如下

@IBOutlet weak var showResult: UILabel!
@IBOutlet weak var num1: UITextField!
@IBOutlet weak var num2: UITextField!
@IBOutlet weak var totalNum: UILabel!
var total = 0
@IBAction func closeKeyboard(_ sender: AnyObject) {//Did End on exit事件
self.view.endEditing(true) //關閉鍵盤方法1
}
@IBAction func closeKeyboard2(_ sender: AnyObject) {//Did End on exit事件
resignFirstResponder() /關閉鍵盤方法2
}
@IBAction func plusButton(_ sender: AnyObject) {
total = Int(num1.text!)! + Int(num2.text!)! //按下button相加
totalNum.text = "\(total)" //顯示字數
self.view.endEditing(true) //關鍵盤
}

就醬

Like what you read? Give AnD a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.