Delegate
Sometimes we require to call methods from one viewController1 to another viewcontroller2 and also get data from viewcontroller2 to viewcontroller1
for this, we need to make the link between two viewcontroller so that can communicate when another viewcontroller completes their work.
here I will show two class where the first class name “Home.swift” and all delegate method declare in “Global.swift” class .
1: first we need to define “Global.swift” class its Parent is NSObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
@objc protocol CastHandlerDelegate: class { func finalHttpDataprocessCompleted(data :NSDictionary) // delegate method 1 func finalCallingApiCompleted() // delegate method 2 } class GlobalData: NSObject{ var delegate:CastHandlerDelegate? func callingHttpPostMethod (params:String,apiname : String ,signalName: String){ let urlString = BASE_DOMAIN + apiname print("url %@",urlString); print("params %@",params); var request = URLRequest(url: URL(string: urlString)!) request.httpMethod = "POST" let postString = params; request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error print("error=\(error)") return } if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors print("statusCode should be 200, but is \(httpStatus.statusCode)") print("response = \(response)") } do { let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject] collection = json as! NSDictionary if(signalName.compare("HttpLoginPostMetod") == .orderedSame){ print("receive final data " , collection); let defaults = UserDefaults.standard; defaults .set(collection["sessionId"], forKey: "sessionId") self.startCallingApi(); } else if(signalName.compare("HttpPostMetod") == .orderedSame){ self.startFinalData(); } //self.startFinalData(); } catch { print("json error: \(error)") } } task.resume() } func startFinalData(){ delegate?.finalHttpDataprocessCompleted(data : collection); // it will call their method } func startCallingApi(){ delegate?.finalCallingApiCompleted(); // it will call their method } } |
2: here I have defined two delegate method (write as the comment) . this method we have to define in Home.swift
3: Now I will show Home.swift class . To call , To receive message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import UIKit class ViewController: UIViewController,CastHandlerDelegate { let globalObjectHome = GlobalData(); override func viewDidLoad() { super.viewDidLoad() globalObjectHome.delegate = self; globalObjectHome.callingHttpPostMethod(params:post as String ,apiname:"mobikulhttp/catalog/getcategoryList",signalName: "HttpPostMetod"); // calling the delegate method } func finalCallingApiCompleted(){ // recive the data from global class } func finalHttpDataprocessCompleted(data :NSDictionary){ // recive the data from global class } } |
4: Now you can use this technique in any classes .