ios - widgetURL 在 Foreach 内被覆盖?

标签 ios arrays swiftui widgetkit

我在 iOS 14 中等大小的 WidgetKit 中显示 3 行,如下所示:

 row 1
-------
 row 2
-------
 row 3
-------

我的 View 结构在这里:

VStack {
    ForEach(records, id: \.id) { item in 
        ZStack {
            // some views
        }
        .widgetURL(URL(string: "wig://\(item.id)"))
    }
    Divider()
}

firstsecond 项的 WidgetKit URL 似乎被第三项覆盖,所有深层链接都将打开第三项的内容。

为从 ForEach 生成的 View 添加深层链接的正确方法是什么?

最佳答案

这里是接口(interface)契约(注意标记处)

    /// Sets the URL to open in the containing app when the user clicks the widget.
    /// - Parameter url: The URL to open in the containing app.
    /// - Returns: A view that opens the specified URL when the user clicks
    ///   the widget.
    ///
>>    /// Widgets support one `widgetURL` modifier in their view hierarchy.
>>    /// If multiple views have `widgetURL` modifiers, the behavior is
    /// undefined.
    public func widgetURL(_ url: URL?) -> some View

你必须使用 Link,比如

    ForEach(records, id: \.id) { item in 
       Link(destination: URL(string: "wig://\(item.id)")!) {
         ZStack {
            // some views
         }
       }
    }

关于ios - widgetURL 在 Foreach 内被覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64345221/

相关文章:

ios - Firebase自定义身份验证问题 token 不同的受众

C - 未初始化的指针 -> 指向数组

SwiftUI 将数据从协调器中获取到内容 View 中

swiftui - 在 SwiftUI 中推送到另一个屏幕时如何隐藏 NavigationView

ios - Swift 中 WebView 的问题

iphone - UIView 动画 block 不工作

Java - 在构造函数中初始化数组

swift 用户界面。如何更改 TextField 的占位符颜色?

ios - 如何将 Cognito 用户池与 Cognito 联合身份池集成

java - 在java中将Object[]数组转换为List