简体   繁体   English

Swift:关于协议和委托模式

[英]Swift: About Protocols and Delegation pattern

i want to ask that how the protocols and delegation patterns functions in Swift. 我想问一下协议和委托模式在Swift中是如何工作的。

I have an application that let me try the google ad sdk on iOS platform. 我有一个可让我在iOS平台上尝试google ad sdk的应用程序。 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. 我要问的是一个混淆的问题:好的,该方法是从代理“ GADInsterstitialDelegate”继承的名为“ interstitialDidDismissScreen”的方法,但是该方法如何通过按插页式广告的关闭按钮进行处理。 Where the engineers of Google implemented and how they succeed this behavior. Google工程师在哪里实施以及他们如何成功实现这种行为。 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. 我怀疑方法interstitialDidReceiveAd(ad: GADInterstitial!)interstitialDidDismissScreen(ad: GADInterstitial!)是委托方法。

When the interstitial object needs to send messages to it's delegate, it calls these methods. interstitial对象需要向其委托发送消息时,它将调用这些方法。

The button handling is taking place inside the GADInterstitial class. 按钮处理在GADInterstitial类内部进行。 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. 我不在乎条形码的读取方式,我只想知道代码,因此我可以将我的调用类设置为条形码读取类的委托,并且当读取条形码时,我会在条形码委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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