Published: 26 Nov 2018 › Updated: 26 Nov 2018

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:
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
- Keyframe animations with Swift
- One algorithm a day: Keeping track of the largest element in a stack
- One algorithm a day. Write a function that returns best profit you could made from trading.
- One algorithm a day: floor of the square root of the input
- iOS Interview: Delegation Pattern in Swift
- Delegation Pattern in Objective-C
- Swift Interview Questions Recursion: Print numbers from 0 to n without using for loop.
- Bitwise Operators in Swift
- S O L I D Principle Rules in iOS
- Decentralized Programmer: Solidity 101
- Decentralized Programmer: My first Erc20 token on Ethereum blockchain!
- How to decentralize your resume
- Decentralized Programmer: Simple KuCoin Trading Bot
- Decentralized Programmer Part 6 My very first Dapp!
- Decentralized Programmer Part 5 Deployment of your contract with Remix IDE!
- Decentralized Programmer Part 4 web3 and specifying value of the transaction
- Decentralized Programmer: Part 3 Remix IDE + Ganache-CLI
- Decentralized Programmer: Part 2 Truffle
- Decentralized Programmer: Part 1 Environment Metamask and Ganache
- Crypto Market Sentiment Update, December 21, 2017