ZTCYING

Lab2- MoneyConverter

May 4th, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.16 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  MoneyConverter
  4. //
  5. //  Created by Sabin Tabirca on 26/01/2024.
  6. //
  7.  
  8. import UIKit
  9.  
  10. @main
  11. class AppDelegate: UIResponder, UIApplicationDelegate {
  12.  
  13.  
  14.  
  15.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  16.         // 应用程序启动后自定义的覆盖点.
  17.         return true
  18.     }
  19.  
  20.     // MARK: UISceneSession Lifecycle 场景会话生命周期
  21.  
  22.     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  23.         // 创建新场景会话时调用。
  24.         // 使用此方法选择用于创建新场景的配置。
  25.         return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  26.     }
  27.  
  28.     func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  29.         // 当用户放弃场景会话时调用。
  30.         // 如果在应用程序未运行时丢弃任何会话,则将在 application:didFinishLaunchingWithOptions 之后不久调用此方法。
  31.         //使用此方法释放特定于已丢弃场景的任何资源,因为它们不会返回。
  32.     }
  33.  
  34.  
  35. }
  36.  
  37. //
  38. //  SceneDelegate.swift
  39. //  MoneyConverter
  40. //
  41. //  Created by Sabin Tabirca on 26/01/2024.
  42. //
  43.  
  44. import UIKit
  45.  
  46. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  47.  
  48.     var window: UIWindow?
  49.  
  50.  
  51.     func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  52.         // 使用此方法可以选择配置 UIWindow `window` 并将其附加到提供的UIWindowScene `scene`。
  53.          // 如果使用故事板,`window` 属性将自动初始化并附加到场景。
  54.          // 此委托并不意味着连接场景或会话是新的(请参阅 `application:configurationForConnectingSceneSession` )。
  55.         guard let _ = (scene as? UIWindowScene) else { return }
  56.     }
  57.  
  58.     func sceneDidDisconnect(_ scene: UIScene) {
  59.         // 系统正在释放场景时调用。
  60.          // 这会在场景进入后台后不久发生,或者在其会话被丢弃时发生。
  61.          // 释放与该场景关联的所有资源,这些资源可以在下次场景连接时重新创建。
  62.          // 场景稍后可能会重新连接,因为其会话不一定被丢弃(请参阅 `application:didDiscardSceneSessions` )。
  63.     }
  64.  
  65.     func sceneDidBecomeActive(_ scene: UIScene) {
  66.         // 当场景从非活动状态转变为活动状态时调用。
  67.          // 使用此方法重新启动场景不活动时暂停(或尚未启动)的任何任务。
  68.     }
  69.  
  70.     func sceneWillResignActive(_ scene: UIScene) {
  71.         // 当场景从活动状态转变为非活动状态时调用。
  72.          // 这可能是由于临时中断(例如来电)而发生的。
  73.     }
  74.  
  75.     func sceneWillEnterForeground(_ scene: UIScene) {
  76.         // 当场景从背景过渡到前景时调用。
  77.          // 使用此方法撤消进入后台时所做的更改。
  78.     }
  79.  
  80.     func sceneDidEnterBackground(_ scene: UIScene) {
  81.         //当场景从前景过渡到背景时调用。
  82.         // 使用该方法保存数据、释放共享资源、存储足够的场景特定状态信息
  83.         // 将场景恢复到当前状态。
  84.     }
  85.  
  86.  
  87. }
  88.  
  89. //
  90. //  ViewController.swift
  91. //  MoneyConverter
  92. //
  93. //  Created by Sabin Tabirca on 26/01/2024.
  94. //
  95.  
  96. import UIKit
  97.  
  98. class ViewController: UIViewController {
  99.    
  100.     // outlets and actions
  101.  
  102.     @IBOutlet weak var euroTextField: UITextField!
  103.     @IBOutlet weak var dollarTextField: UITextField!
  104.    
  105.     @IBAction func clearAction(_ sender: Any) {
  106.         // clear the fields
  107.         euroTextField.text = ""
  108.         dollarTextField.text    = ""
  109.     }
  110.    
  111.     @IBAction func convertAction(_ sender: Any) {
  112.         if euroTextField.text != "" && dollarTextField.text == ""{
  113.             // get the value from euroTextField
  114.             let euroAmount = Double(euroTextField.text!)
  115.            
  116.             // convert it to dollars
  117.             let dollarAmount = convert(amount: euroAmount!, rate: convertRate)
  118.            
  119.             // put the dollar value to dollarTextField
  120.             dollarTextField.text = "converted \(dollarAmount)"
  121.         }
  122.         else if euroTextField.text == "" && dollarTextField.text != ""{
  123.             // get the value from dollarTextField
  124.             let dollarAmount = Double(dollarTextField.text!)
  125.            
  126.             // convert it to euros
  127.             let euroAmount = convert(amount: dollarAmount!, rate: 1/convertRate)
  128.            
  129.             // put the euros value to euroTextField
  130.             euroTextField.text = "converted \(euroAmount)"
  131.            
  132.         }
  133.     }
  134.    
  135.    
  136.     // vars and methods
  137.     let convertRate = 1.234
  138.    
  139.     func convert(amount:Double, rate:Double)->Double
  140.     {
  141.         let convertedAmount = amount * rate
  142.         return convertedAmount
  143.     }
  144.    
  145.  
  146.     override func viewDidLoad() {
  147.         super.viewDidLoad()
  148.         // Do any additional setup after loading the view.
  149.     }
  150.  
  151.  
  152. }
  153.  
  154.  
Add Comment
Please, Sign In to add comment