Auto fill another textfield, More than Two texfield Swift 4 (Use In API )

Here is example code :

1.  TextDidEndEditing (after you end editing text)

 import UIKit
import Alamofire
import SwiftyJson
 
class autoTextViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var txt1: UITextField! @IBOutlet weak var txt2: UITextField! @IBOutlet weak var txt3: UITextField! let myArray = "String1" let myArray2 = "String2" override func viewDidLoad() { super.viewDidLoad() txt1.delegate = self txt2.delegate = self txt3.delegate = self txt1.resignFirstResponder() // to respon text end editing }
func textFieldDidEndEditing(_ textField: UITextField) { DispatchQueue.main.async { NetworkService<WowMobilJenisEntity>.process(client: NetworkClient(endPont: .jenisMobil(mobilId: self.merkIdM)), onSuccess: { dataJ in var data: [PickerDataSource] = [] self.txt2.text = dataJ.data?.first?.jenis ?? "" }, onFailure: nil) self.txt3.text = self.myArray2 }
     }
}

2. Use In API  (This exaple use Post and GET data)



import UIKit

class autoTextViewController: UIViewController, UITextFieldDelegate {
 
    
    @IBOutlet weak var txt1: UITextField!
    @IBOutlet weak var txt2: UITextField!
    
  
    var merkIdM: Int = 0
    var tipeId: Int = 0
    
    enum FieldType {
        case merk, tipe
    }
    var fieldType: FieldType = .merk
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
         txt1.delegate = self
        txt2.delegate = self
    
        
        txt1.resignFirstResponder()

       
    }
   
    
   
    @IBAction func txt1DidEnd(_ sender: UITextField) {
        self.fieldType = .merk 
      let param = ["dataSend": txt1.text]
Alamofire.request("Url", method: .post, parameters: param, encoding: URLEncoding.queryString, headers: nil) .responseJSON { response in switch response.result { case .success(let value): case .failure: print("Error") } } } @IBAction func txt2DidEnd(_ sender: UITextField) { self.fieldType = .tipe sender.resignFirstResponder()
     
} func textFieldDidEndEditing(_ textField: UITextField) { var tipe: String? init(json: JSON) { self.id = json["tipe"].string } Alamofire.request("Url", method: .get, parameters: nil, encoding: URLEncoding.queryString, headers: nil) .responseJSON { response in switch response.result { case .success(let value): self.txt2.text = value.data?.first?.tipe ?? "" case .failure: print("Error") }
            }  
    
   }
} extension autoTextViewController: CustomPickerDelegate { func didSelect(data: PickerDataSource?) { switch self.fieldType { case .merek: self.merkView.isiLabel.text = data?.name ?? "" self.merkIdM = Int(data?.id ?? "0")! case .jenis: self.txt2.text = data?.name ?? "" self.tipeId = Int(data?.id ?? "0")! } } }













No comments: