In Swift, delegates are implemented using a protocol declaration
For example, if you have two classes, a parent and a child:
class Parent {
}
class Child {
}
And if you want to notify the Parent, a change from Child, you would use protocol:
Create A Protocol:
protocol ChildDelegate {
func childDidSomething()
}
In the child class we need to declare a property to store the reference to the delegate like below
class Child {
weak var delegate: ChildDelegate?
}
Notice the variable delegate is an optional and the protocol ChildDelegate is marked to be only implemented by class type (without this the delegate variable can’t be declared as a weak reference avoiding any retain cycle. This means that if the delegate variable is no longer referenced anywhere else, it will be released)
In this example, when Child class did something and needs to notify its Parent class, then Child class will call as
delegate?.childDidSomething()
If the delegate has been defined, the delegate will be notified child did something.
The parent class will need to extent the ChildDelegate protocol to be able to respond to its actions. This can be done directly on the parent class:
class Parent: UiViewController, ChildDelegate {
func childDidSomething() {
print(“Child did something”)
}
}
That’s it.
——————————-Still———————————-
——————————Confused——————————
———————————?———————————–
Want to go further example ?
Create a New Project
How to create New Project? :
Open Xcode from Applications- Go to File – New Project – Name your project and Save
In Xcode you will see AppDelegate, View Controller File’s and Storyboard on left side
Then Open StoryBoard
-> Drag UITableView to the ViewController
-> Fit that Tableview to ViewController frame
-> Set Constraints to all bounds of Tableview using Pin
I took Child class as CustomTableViewCell and ViewController as Parent
I have a BuyButtonAction in UITableViewCell(Child) after Clicking BuyButton in TableviewCell, Payment Method will call in ViewController Class(Parent) using Protocol
First Create Protocol
Syntax:
protocol <#name#> {
<#requirements#>
}
protocol CustomTableViewCellDelegate{ //CustomDelegate Name
func buyTapped(cell: CustomTableViewCell) //Your CustomDelegate Method Name
}
Child Class :
I am going to load bunch of styles in UITableViewCell where it will have Buy button to purchase that product
First create a CustomClass subclass of UITableViewCell
class CustomTableViewCell: UITableViewCell {
@IBOutlet var priceLbl: UILabel!
@IBOutlet var styleImageView: UIImageView!
//DECLARE your protocol
var delegate:CustomTableViewCellDelegate?
@IBAction func payButtonAction(_ sender: UIButton) {
delegate?.buyTapped(cell: self) // Call wherever you need to call
}
}
Parent Class:
import UIKit
//Assign your Delegate like here 👇
class ViewController: UIViewController, CustomTableViewCellDelegate {
@IBOutlet var tableView: UITableView!
//Load Images to Assets and load Image Names in this array
let stylesImagesArray = [“Style1″,”Style2″,”Style3″,”Style4″,”Style5″,”Style6”];
//Price Array of each Style
let stylePriceArray = [“11.29″,”12.29″,”13.39″,”14.49″,”15.59″,”16.69”];
//YOUR CUSTOM DELEGATE METHOD
func buyTapped(cell:CustomTableViewCell)
{
let indexPath = tableView.indexPath(for: cell) //as! NSIndexPath
let item = stylePriceArray[(indexPath?.row)!]
print(item)
print(“Tapped Buy Button”)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stylesImagesArray.count
}
Don’t forget to assign delegate to self
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “TableCell”, for: indexPath) as! CustomTableViewCell
cell.styleImageView.image = UIImage(named: stylesImagesArray[ indexPath.row])
cell.priceLbl.text = stylePriceArray[indexPath.row]
cell.delegate = self //Don’t forget to assign delegate to self because delegate has been defined in Child class
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
That’s it. Start using Protocols
Source and Reference: Swift Topics
You will come to know Basic working of Apple Pay in Source Code Download Full Project
can you upload SQLite in swift
LikeLike