小编Mir*_*ira的帖子

使用绑定/演示模式从 SwiftUI 中关闭 UIViewController 表?

我有一个 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)

uikit admob modalviewcontroller swift swiftui

5
推荐指数
0
解决办法
628
查看次数

如何在 SwiftUI 中将 Date 转换为 DateComponents?

我想在用户从 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)

notifications datepicker uikit swift swiftui

2
推荐指数
1
解决办法
3843
查看次数