- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过从 UIViewController 更改环境对象并将其传递给 SwiftUI View 来关闭广告时将变量 adsWatched 增加 1。我已经尝试将环境对象放入我的 NSObject 类 AdManager 中,其中包含我想要更改环境对象的函数,但我收到错误消息“Unknown attribute 'EnvironmentObject'”:
public protocol AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber)
func rewardAdFailedToLoad()
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
)
func rewardAdDidOpen()
func rewardAdDidClose()
func rewardAdFailedToPresent()
}
//default implementation AdManagerRewardDelegate
public extension AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber) {}
func rewardAdFailedToLoad() {}
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
) {
if rewardedAd?.isReady == true {
if let rewardViewController = rewardViewController {
rewardedAd?.present(fromRootViewController: rewardViewController, delegate: delegate)
}
}
}
func rewardAdDidOpen() {}
func rewardAdDidClose() {}
func rewardAdFailedToPresent() {}
}
public class AdManager: NSObject {
public static let shared = AdManager()
public var ADS_DISABLED = false
public var delegateReward: AdManagerRewardDelegate?
private var viewController:UIViewController?
private var bannerViewContainer:UIView?
private var rewardViewController:UIViewController?
var interestial:GADInterstitial?
private var testDevices:[String] = [""]
private var rewardedAd: GADRewardedAd?
let borderSizeBetweenBannerAndContent:CGFloat = 5
@EnvironmentObject var settings: UserSettings
public override init() {
super.init()
}
public func configureWithApp(){
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = testDevices
}
public func setTestDevics(testDevices: [String]){
self.testDevices = testDevices
self.testDevices += [kGADSimulatorID as! String ] //all simulator
}
private func getGADRequest() -> GADRequest{
let request = GADRequest()
return request
}
// MARK:- Reward Video Ads
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController){
self.rewardViewController = viewController
rewardedAd = GADRewardedAd(adUnitID: adUnit)
rewardedAd?.load(getGADRequest()) { error in
if let error = error {
// Handle ad failed to load case.
print("Reward based video ad failed to load. \(error.debugDescription)")
self.delegateReward?.rewardAdFailedToLoad()
} else {
// Ad successfully loaded.
print("Reward based video ad is received.")
self.delegateReward?.rewardAdDidReceive(
rewardViewController: self.rewardViewController,
rewardedAd: self.rewardedAd,
delegate: self
)
}
}
}
}
// MARK:- GADRewardBasedVideoAdDelegate
extension AdManager : GADRewardedAdDelegate {
public func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad presented.")
delegateReward?.rewardAdDidOpen()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
delegateReward?.rewardAdFailedToPresent()
}
public func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad dismissed.")
// i want to increase adsWatched by 1 here
settings.adsWatched += 1
delegateReward?.rewardAdDidClose()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
delegateReward?.rewardAdGiveRewardToUser(type: reward.type, amount: reward.amount)
}
}
我已尝试将对象从我的 View Controller 传递给它,但我收到错误消息:“类型为‘View’的实例成员‘environmentObject’不能用于嵌套类型‘Ads.ViewController’的实例”这个:
struct Ads: UIViewControllerRepresentable {
@EnvironmentObject var settings: UserSettings
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return ViewController()
}
func updateUIViewController(_ uiView: UIViewController, context: Context) {
}
class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
var rewardedAd: GADRewardedAd?
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@EnvironmentObject var settings: UserSettings
override func viewDidLoad() {
super.viewDidLoad()
AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, environmentObject(settings))
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}
}
}
有什么方法可以更改 NSObject 中的变量吗?
最佳答案
这里的设置
只是一个引用,所以你可以通过参数传递它
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController,
settings: UserSettings) {
self.rewardViewController = viewController
// ... other code
}
在你的代表中
struct Ads: UIViewControllerRepresentable {
@EnvironmentObject var settings: UserSettings
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return ViewController(settings: settings, mode: presentationMode)
}
func updateUIViewController(_ uiView: UIViewController, context: Context) {
}
class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
var rewardedAd: GADRewardedAd?
var presentationMode: Binding<PresentationMode>
var settings: UserSettings
init(settings: UserSettings, mode: Binding<PresentationMode>) {
self.settings = settings
self.presentationMode = mode
}
override func viewDidLoad() {
super.viewDidLoad()
AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, settings: settings)
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}
}
}
关于swift - 如何从 NSObject 访问 SwiftUI 环境对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62522253/
SWIFT代码 print("1", NSObject() == NSObject()) print("2", ObjectIdentifier(NSObject()) == ObjectIdenti
保留在 NSObject 协议(protocol)中声明。 因此NSObject类和NSProxy类实现了它。 然而 NSProxy 和 NSObject 类都有一个分配。 为什么 alloc 没有在
我有一个已在App Store中发布的应用程序。大约有1%-2%的用户报告该应用程序崩溃了。这不是完全意外的行为,因此我要求提供崩溃日志。这是最后一个异常回溯(实际显示问题的部分): Last E
我有一个自定义 NSObject 类,可以调用 People,还有一个来自 CloudMade RMMarker.h 的名为 RMMarker 的类。 RMMarker 类有一个名为 data 的属性
这是崩溃报告,不知道为什么。我使用 AFHTTPRequestOperation setCompletionBlockWithSuccess:failure: 调用电话。在完成 block 中,我得到
应用程序有时会因 [NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃,选择器为 [UIImageView setImage:]。 我通过设置异常断点捕
崩溃报告: 0 CoreFoundation!__exceptionPreprocess + 0x7c 1 libobjc.A.dylib!objc_exception_throw
我正在将我的整个项目从 Objective-C 转换为 Swift...在转换时我遇到了这个问题:“无法将‘Facility’类型的值转换为预期的参数类型“Facility!” 错误出现在我提到的最后
我在将数组[String]保存到核心数据时遇到问题。在核心数据中,我设置了字段“countires”的类型 - 可转换。 我通过以下方式添加数据: filters!.countries = selec
假设您有一个返回 NSDictionary 的方法。要构建字典,您可能需要创建一个 NSMutableDictionary。返回字典的不可变副本而不是仅返回可变字典是否有任何必要或优势? 例如 - (
在一个小型 RTS 项目中,我有一个按钮列表,每个按钮都分配有一个构建对象。有些建筑物非常通用,但其他建筑物则非常特殊,因此我将某些建筑物作为子类。 当我创建一个按钮列表时,每个按钮都有一个 PEHo
这个问题在这里已经有了答案: Objective-C: Property / instance variable in category (6 个答案) Objective-C: Instance
我实际上想知道如何从 NSObject 或任何其他类访问 encodeWithCoder、init 等方法而不继承 NSObject 类。 因为我读到如果我们从 NSObject 类继承,那么它在 S
我正致力于从 Objective-C 教程转换此委托(delegate)协议(protocol)函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何
在我的 Swift 应用程序中,我有一个类: open class CustomCluster : NSObject { open var coordinate = CLLocationCoo
我有一个字典/数组,看起来像这样: var myArray: [[String:NSObject]] = [] let newItem = [ [
这很奇怪,我可以使用 Xcode 5 (5A1413) 在模拟器、iPhone 4S 和 iPhone 5 中运行我的应用程序而没有任何问题,但是当我将应用程序提交到 iTunes 商店时被拒绝并且此
我使用 NSKeyedArchiver.archivedDataWithRootObject(obj) 将对象转换为 NSData。 archivedDataWithRootObject(obj) 方
背景。 请考虑以下步骤: 1) 在 Xcode 中创建一个新的“单 View 应用程序”。 2)创建类NSObject+Extension.h和.m文件: // .h @interface NSObj
在 ARC 环境中遇到一些小问题。创建一个将 View 添加到父 View 的 NSObject - 它基本上是一个可以处理一些文本并显示它的“弹出类”。 在 View Controller 中它被实
我是一名优秀的程序员,十分优秀!