【问题标题】:Objective C Delegate in SwiftSwift 中的 Objective C 委托
【发布时间】:2016-05-26 19:01:48
【问题描述】:

我刚刚在我的所有 swift 应用程序中实现了 Chartboost。

我奖励了插页式广告。所以我需要使用 Chartboost 给我的目标 C 函数来设置一个委托。我不知道该怎么做。这就是我通过 Swift 做 Admob 的方式。

   NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentInterstitial:", name: "AdMobCall", object: nil) 

   func presentInterstitial(sender:UIButton!) {

    if let isReady = interstitial?.isReady {   // _ isReady
        interstitial?.presentFromRootViewController(self)

    }
}

 NSNotificationCenter.defaultCenter().postNotificationName("AdMobCall", object: nil)

但是给我的Objective C Chartboost函数是:

  • (void)didCompleteRewardedVideo:(CBLocation)位置 withReward:(int)reward;

而且我不明白如何使用此函数创建与 Admob 相同的委托。这似乎不可能。我让 Chartboost 奖励插页式广告正常工作。但是我无法设置代理来查看他们是否观看了视频。

【问题讨论】:

    标签: objective-c swift delegates chartboost


    【解决方案1】:

    如果你想实现objective-c委托到swift。您需要执行以下步骤。

    1. 需要创建桥文件来导入你的 Objective-C 类

      //
      //  Use this file to import your target's public headers that you would like to expose to Swift.
      //
      
      #import "UVIViewController.h"
      
    2. 您需要在 swift viewcontroller 中扩展协议名称

      import UIKit
      
      class ViewController: UVIViewController, UVIDataRefreshProtocol {   // 2. Extend Protocal
      
          var secondController: DelegateViewController?
      
          @IBOutlet var delegateRefresh: UILabel!
      
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
      
              self.registerForBasicDataRefreshNotification();             //  3. Register the protocol notification
          }
      
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
              // Dispose of any resources that can be recreated.
          }
      
          // Register the protocol notification with same name. Example : DataRefreshNotification
          override func registerForBasicDataRefreshNotification() {
      
              self.validateBasicDataRefreshNotification();
              NSNotificationCenter.defaultCenter().addObserver(        // 4. Register the protocol notification with same name.
                  self,
                  selector: "basicDataDidRefresh",
                  name: DataRefreshNotification,
                  object: nil)
      
          }
      
          override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
              delegateRefresh.text = "";
              if segue.identifier == "view_to_delegate" {
                  secondController = segue.destinationViewController as? DelegateViewController
              }
          }
      
          // Basic Data Did Refresh Notification
          func basicDataDidRefresh() {                            //  4. Event will be triggered while calling the protocol post notification. called with same name
                  NSLog("\n Basic data is refreshed")
                  delegateRefresh.text = "Basic data is refreshed";
          }
      
      }
      

        5. NSNotificationCenter.defaultCenter().postNotificationName(DataRefreshNotification, object: nil)  // Call the notification.
    

    示例项目:https://github.com/vigneshuvi/Objective-C-Delegate-Swift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多