我有一个 SwiftUI 视图,其中包含一个指向 UIViewControllerRepresentable 结构的工作表,该结构使用 Google Ad Manager 扩展。我希望 UIKit 扩展在使用演示模式运行完成后关闭该工作表,但通过环境使用演示模式不起作用。代码是否未按照我设置的方式运行,或者是否无法从 Google Ads Manager 中关闭该工作表?
或者,当我尝试使用 Binding 关闭工作表以更新 AdManager 中的 isPresented 时,我收到错误消息:“属性 'self.isPresented' 未在 super.init 调用中初始化”。然而,当我尝试初始化它时,它说我不能在 super.init 之前执行它,而当我将它放在 super.init 之后时,它说我不能在 super.init 之后调用它。广告加载完成后,有什么方法可以关闭该工作表(函数rewardedAdDidDismiss)?
编辑:这篇文章最初被标记为重复,但链接的问题询问如何从 UIViewController 中消除 SwiftUI 模式,而不是相反。
SwiftUI 视图:
import SwiftUI
struct AdRevenue: View {
@State var playAd = false
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(action: {
self.playAd = true
})
{
Text("Play Ad")
}.sheet(isPresented: $playAd) {
Ads()}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 UIViewControllerRepresentable:
import GoogleMobileAds
import SwiftUI …Run Code Online (Sandbox Code Playgroud) 我想在用户从 DatePicker 中选择时设置每日通知,但我不知道如何将选定的小时和分钟转换为 DateComponents 以匹配 UNCalendarNotificationTrigger。
这是我的日期选择器:
DatePicker("Choose time of notif", selection: $notifTime, displayedComponents: .hourAndMinute)
Run Code Online (Sandbox Code Playgroud)
这是我的通知触发器:
var dateComponents = DateComponents()
dateComponents = notifTime
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
Run Code Online (Sandbox Code Playgroud)