6

我创建了一个 SwiftUI 视图,它显示在 MKMapView 的注释标注中。我使用 UIHostingController 来包装 SwiftUI 视图,并使用 annotationView 的detailCalloutAccessoryView属性来设置视图并显示它。这在大多数情况下都可以正常工作。然后我在 SwiftUI 视图中添加了一些按钮,点击这些按钮时需要显示警报或操作表。

这是代码:

struct ContactProfileButton: View {
    @State var showActionSheet: Bool = false
    var body: some View {
        Button(action: {
            print("Profile button tapped!")
            self.showActionSheet.toggle()
        }){
            Image("profile").resizable()
        }.frame(width: 26.0, height: 26.0)
        .actionSheet(isPresented: $showActionSheet, content: {
            print("Profile button - show action sheet")
            return ActionSheet(title: Text("Profile title"),
                               message: Text("Profile message"),
                               buttons: [
                                .default(Text("Do something")),
                                .destructive(Text("Cancel"))
                               ])
        })
    }
}

当我点击按钮时,它可以正常工作并显示一个操作表,但会在控制台中生成此警告:

[演示] 不鼓励从分离的视图控制器 < TtGC7SwiftUI19UIHostingControllerVS_7AnyView : 0x7fac1fac8280> 呈现视图控制器 <SwiftUI.PlatformAlertController: 0x7fac118b3a00>。

解决此警告的最佳方法是什么?如果它托管在 UIHostingController 中,我是否应该在 SwiftUI 视图中显示警报或 ActionSheet?还是我应该以其他方式这样做?

4

0 回答 0