简体   繁体   中英

Swift: About Protocols and Delegation pattern

i want to ask that how the protocols and delegation patterns functions in Swift.

I have an application that let me try the google ad sdk on iOS platform. But i'm missing something and confused about how the methods works.

I have some codes like these;

import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADInterstitialDelegate {

@IBOutlet weak var bannerView: GADBannerView!
let request = GADRequest()
var interstitial: GADInterstitial!

@IBOutlet weak var mylbl: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    bannerView.adUnitID = "xxx"
    bannerView.rootViewController = self
    bannerView.loadRequest(self.request)
    interstitial = createAndLoadInterstitial()
}

func createAndLoadInterstitial() -> GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "xxx")
    interstitial.delegate = self
    interstitial.loadRequest(self.request)
    return interstitial
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    interstitial = createAndLoadInterstitial()
    mylbl.text = "No ad"

}

func interstitialDidReceiveAd(ad: GADInterstitial!) {
    mylbl.text = "received ad"
}

@IBAction func touched(sender: AnyObject) {

    if interstitial.isReady
    {
        interstitial.presentFromRootViewController(self)
    }
    else
    {
        mylbl.text = "Not Ready!"
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

For the code above, i'm aware of that the protocols blueprints of methods and properties to adopt a class or struct or enum. The methods or properties defined in the protocol should be implemented on the class that adopted by the related delegate.

I want to ask that and cofused point: OK the method which is named "interstitialDidDismissScreen" inherited from the delegate "GADInsterstitialDelegate" but how the method handled by pressing the close button of the interstitial ad. Where the engineers of Google implemented and how they succeed this behavior. Thanks for your help.

Good hacks,

The wording of your question is garbled and hard to figure out.

A protocol is basically a contract. It says that objets that conform to the protocol promise to provide the properties, and respond to the methods that the protocol defines.

When you say

someObject.delegate = self

You are passing a pointer to yourself to the other object. This is like giving somebody your phone number and saying "Please run these errands for me. If you have any questions, call me at this number. Also please call me when the errands are done."

Since the other object knows that it's delegate conforms to a specific protocol, it knows what messages it can send over the phone (what messages it can send to the delegate)

I suspect the methods interstitialDidReceiveAd(ad: GADInterstitial!) and interstitialDidDismissScreen(ad: GADInterstitial!) are delegate methods.

When the interstitial object needs to send messages to it's delegate, it calls these methods.

The button handling is taking place inside the GADInterstitial class. When they setup the class they probably have some internal methods that handle all the ad interaction, and then using the delegate methods they send back to your class the info you need to know to keep your UI managed. By implementing the delegate and its methods you've said I want to use something that your class does, and then I want to also handle all the feedback from that class. If you were to make your own class and implement a protocol and delegate you could do whatever you want inside your class and then pass back just a sliver of info to the class' delegate. An example would be a barcode reading class. I don't care how the barcode gets read, I just want to know the code, so I could set my calling class to be the delegate of the barcode reading class, and when the barcode is read I would receive the barcode back inside of a barcode delegate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM