Advertisement
ZTCYING

Lab4- MultipleConverter

May 4th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.01 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  MultipleConverter
  4. //
  5. //  Created by Sabin Tabirca on 06/02/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.         // Override point for customization after application launch.
  17.         return true
  18.     }
  19.  
  20.     // MARK: UISceneSession Lifecycle
  21.  
  22.     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  23.         // Called when a new scene session is being created.
  24.         // Use this method to select a configuration to create the new scene with.
  25.         return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  26.     }
  27.  
  28.     func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  29.         // Called when the user discards a scene session.
  30.         // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  31.         // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  32.     }
  33.  
  34.  
  35. }
  36.  
  37. //
  38. //  SceneDelegate.swift
  39. //  MultipleConverter
  40. //
  41. //  Created by Sabin Tabirca on 06/02/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.         // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  53.         // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  54.         // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  55.         guard let _ = (scene as? UIWindowScene) else { return }
  56.     }
  57.  
  58.     func sceneDidDisconnect(_ scene: UIScene) {
  59.         // Called as the scene is being released by the system.
  60.         // This occurs shortly after the scene enters the background, or when its session is discarded.
  61.         // Release any resources associated with this scene that can be re-created the next time the scene connects.
  62.         // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
  63.     }
  64.  
  65.     func sceneDidBecomeActive(_ scene: UIScene) {
  66.         // Called when the scene has moved from an inactive state to an active state.
  67.         // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
  68.     }
  69.  
  70.     func sceneWillResignActive(_ scene: UIScene) {
  71.         // Called when the scene will move from an active state to an inactive state.
  72.         // This may occur due to temporary interruptions (ex. an incoming phone call).
  73.     }
  74.  
  75.     func sceneWillEnterForeground(_ scene: UIScene) {
  76.         // Called as the scene transitions from the background to the foreground.
  77.         // Use this method to undo the changes made on entering the background.
  78.     }
  79.  
  80.     func sceneDidEnterBackground(_ scene: UIScene) {
  81.         // Called as the scene transitions from the foreground to the background.
  82.         // Use this method to save data, release shared resources, and store enough scene-specific state information
  83.         // to restore the scene back to its current state.
  84.     }
  85.  
  86.  
  87. }
  88.  
  89. //
  90. //  ViewController.swift
  91. //  MultipleConverter
  92. //
  93. //  Created by Sabin Tabirca on 06/02/2024.
  94. //
  95.  
  96. import UIKit
  97.  
  98. class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
  99.    
  100.     // picker view methods
  101.     func numberOfComponents(in pickerView: UIPickerView) -> Int {
  102.         return 1
  103.     }
  104.    
  105.     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  106.         return currencyNames.count
  107.     }
  108.    
  109.     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  110.         return currencyNames[row]
  111.     }
  112.    
  113.     // outlets and actions
  114.    
  115.     @IBOutlet weak var fromTextField: UITextField!
  116.     @IBOutlet weak var toTextField: UITextField!
  117.     @IBOutlet weak var fromPicker: UIPickerView!
  118.     @IBOutlet weak var toPicker: UIPickerView!
  119.    
  120.     @IBAction func clearAction(_ sender: UIButton) {
  121.         // clear the text fields
  122.         fromTextField.text = ""
  123.         toTextField.text   = ""
  124.        
  125.         // reset the pickers
  126.         fromPicker.selectRow(0, inComponent: 0, animated: true)
  127.         toPicker.selectRow(0, inComponent: 0, animated: true)
  128.     }
  129.    
  130.    
  131.     @IBAction func convertAction(_ sender: UIButton) {
  132.         // get the selected indices from pickers
  133.         let fromIndex = fromPicker.selectedRow(inComponent: 0)
  134.         let toIndex   = toPicker.selectedRow(inComponent: 0)
  135.        
  136.         // calculate the rate
  137.         let rate = currencyRates[toIndex]/currencyRates[fromIndex]
  138.        
  139.         // do convertion
  140.         let fromAmount = Double(fromTextField.text!)!
  141.         let toAmount   = convert(amount: fromAmount, rate: rate)
  142.        
  143.         toTextField.text = String(format: "%.2lf", toAmount)
  144.        
  145.        
  146.     }
  147.    
  148.     // user vars and methods
  149.     let currencyNames = ["EUR", "USD", "UKP", "RON", "CNY", "INR", "TRY"]
  150.     let currencyRates = [1.0,   1.23,  0.89,  5.12,   7.50,  89.15,  32.12]
  151.    
  152.     func convert(amount:Double, rate:Double)->Double{
  153.         return amount * rate
  154.     }
  155.    
  156.     override func viewDidLoad() {
  157.         super.viewDidLoad()
  158.         // Do any additional setup for the pickers
  159.         fromPicker.delegate   = self
  160.         fromPicker.dataSource = self
  161.         toPicker.delegate     = self
  162.         toPicker.dataSource   = self
  163.     }
  164.  
  165.  
  166. }
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement