我在 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()
}
first 和 second 项的 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/