期望:我想在Delegates方法调用时执行一些操作Observed:Delegates方法没有成功调用屏幕上的Ad's show 的错误 代码:Chartboost.delegate =自
错误:类型'Chartboost'没有成员'委托'
的AppDelegate
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
containerWidthConstraintForLandscape = NSLayoutConstraint(item: containerView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.5, constant: 0)
NSLayoutConstraint.activate([containerWidthConstraintForLandscape])
NSLayoutConstraint.deactivate([containerWidthConstraintForPortrait])
}
else {
containerWidthConstraintForPortrait = NSLayoutConstraint(item: containerView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.7, constant: 0)
NSLayoutConstraint.deactivate([containerWidthConstraintForLandscape])
NSLayoutConstraint.activate([containerWidthConstraintForPortrait])
}
}
ViewController代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Chartboost.start(withAppId: "4f21c409cd1cb2fb7000001b", appSignature: "92e2de2fd7070327bdeb54c15a5295309c6fcd2d", delegate: nil)
return true
}
答案 0 :(得分:0)
如果委托设置为nil,则调用委托方法(在本例中为Chartboost)的类将无法进行委托方法调用。您应该将代理人设置为“自己”'您已实现Chartboost预期的委托方法的类。
在上面的示例中,您可以将Chartboost委托设置为' self' ViewController。
例如,在ViewController中,您已经声明了' ChartboostDelegate'在班级签名。当您想要启用Chartboost委托方法时,请分配ViewController的自我'使用类似以下内容的Chartboost委托:
Chartboost.delegate = self
对于Chartboost,看起来作者将委托设为私有,因此可以使用以下命令在ViewController中设置:
Chartboost.start(withAppId: "some uid", appSignature: "some other uid", delegate: self)
或者,后来发现:
Chartboost.setDelegate(self)
(也可以通过在故事板中查找ViewController实例来在AppDelegate类中设置。在这种情况下不太合适。)
如果您在生成委托方法的调用签名存根(委托所期望的方法调用)时遇到问题,XCode会为您自动生成它们。只需单击类声明旁边的错误消息:
Type '<your class implementing the delegate methods>' does not conform to protocol '<the delegate protocol to implement>'
将出现有关错误的更多详细信息。点击&#39;修复&#39;按钮和XCode将为您自动生成方法存根。
答案 1 :(得分:0)
必须使用Chartboost.setDelegate(self)
将委托设置为self func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Chartboost.start(withAppId: "4f21c409cd1cb2fb7000001b", appSignature: "92e2de2fd7070327bdeb54c15a5295309c6fcd2d", delegate:self as ChartboostDelegate)
Chartboost.setDelegate(self as ChartboostDelegate)
return true
}
在查看如何在Swift中正确转换Objective-C方法之后,我添加了下划线(_),它将函数更改为:
func shouldDisplayRewardedVideo(_ location: CBLocation) -> Bool
{
return true
}
func shouldRequestInterstitial(_ location: CBLocation) -> Bool {
return true
}
然后,XCode给了我一个暗示,我接近委托方法,但需要改变位置的类型,我最终得到了
func shouldDisplayRewardedVideo(_ location: String) -> Bool
{
return true
}
func shouldRequestInterstitial(_ location: String) -> Bool {
return true
}