【问题标题】:widgetURL is override inside Foreach?widgetURL 在 Foreach 内被覆盖?
【发布时间】:2021-01-28 09:10:35
【问题描述】:

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

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

我的视图结构在这里:

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

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

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

【问题讨论】:

    标签: ios arrays swiftui widgetkit


    【解决方案1】:

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

        /// 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
             }
           }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      相关资源
      最近更新 更多