Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. //
  2. // TipViewController.swift
  3. // 只要我長大
  4. //
  5. // Created by yuanchen on 2017/3/25.
  6. // Copyright © 2017年 yuanchen. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class TipViewController: UIViewController {
  12.  
  13. @IBOutlet weak var money: UITextField!
  14.  
  15. @IBOutlet weak var tip: UITextField!
  16.  
  17. @IBOutlet weak var result: UILabel!
  18.  
  19. @IBAction func cilculate(_ sender: Any) {
  20. money.resignFirstResponder()
  21. tip.resignFirstResponder()
  22.  
  23. result.isHidden = false
  24.  
  25. if money.text == "" {
  26. result.text = "0"
  27. }
  28. else{
  29. if tip.text == "" {
  30. result.text = money.text
  31. }
  32. else {
  33. result.text = String(Int(money.text!)! * Int(tip.text!)! / 100)
  34. }
  35. }
  36. }
  37.  
  38. //按按鈕收鍵盤
  39.  
  40. func textFieldShouldReturn(_ textField: UITextField) -> Bool{
  41. money.resignFirstResponder()
  42. tip.resignFirstResponder()
  43. return true
  44. }
  45.  
  46. //按任意地方收鍵盤
  47. func hideKeyboardWhenTappedAround() {
  48. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(TipViewController.dismissKeyboard))
  49. view.addGestureRecognizer(tap)
  50. }
  51.  
  52. func dismissKeyboard() {
  53. view.endEditing(true)
  54. }
  55.  
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58.  
  59. // Do any additional setup after loading the view.
  60. }
  61.  
  62. override func didReceiveMemoryWarning() {
  63. super.didReceiveMemoryWarning()
  64. // Dispose of any resources that can be recreated.
  65. }
  66.  
  67.  
  68. /*
  69. // MARK: - Navigation
  70.  
  71. // In a storyboard-based application, you will often want to do a little preparation before navigation
  72. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  73. // Get the new view controller using segue.destinationViewController.
  74. // Pass the selected object to the new view controller.
  75. }
  76. */
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement