cryptoizotx avatar

iOS Interview: Delegation Pattern in Swift

cryptoizotx

Published: 26 Nov 2018 › Updated: 26 Nov 2018iOS Interview: Delegation Pattern in Swift

iOS Interview: Delegation Pattern in Swift

It's a continuation of my previous post about implementing delegate pattern in iOS. This time it is written in Swift.

App Delegate

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TapProtocol{
    func buttonTapped() {
        print("Button Tapped")
    }
    
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if let w =  self.window,let vc = w.rootViewController as? ViewController
        {
            vc.delegate = self
        }
        return true
    }
}

View Controller

protocol TapProtocol:class {
    func buttonTapped()
}

class ViewController: UIViewController {
    weak var delegate:TapProtocol?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func buttonTapped(_ sender: Any) {
        self.delegate?.buttonTapped()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Leave iOS Interview: Delegation Pattern in Swift to:

Written by

Crypto Trader / Software Engineer

Read more #ios posts


Best Posts From cryptoizotx

We have not curated any of cryptoizotx's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From cryptoizotx