目标C在Swift中委派

时间:2016-05-26 19:01:49

标签: objective-c swift delegates chartboost

我刚刚将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函数是:

  • (无效)didCompleteRewardedVideo:(CBLocation)位置               withReward:(INT)奖励;

我不明白如何使用此功能为Admob创建相同的委托。这似乎不可能。我有Chartboost奖励的部门工作。但我无法设置代表,看看他们是否观看了视频是否已完成。

1 个答案:

答案 0 :(得分:0)

如果你想实现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";
        }
    
    }
    
  3.     5. NSNotificationCenter.defaultCenter().postNotificationName(DataRefreshNotification, object: nil)  // Call the notification.
    

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