widgetURL 在 Foreach 中被覆盖了吗?
widgetURL is override inside Foreach?
我在 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()
}
first 和 second 项的小部件 URL 似乎被第三项覆盖,所有深度 link 将打开第三项的内容。
为从 ForEach 生成的视图添加深度 link 的正确方法是什么?
这里是接口契约(标注处注意)
/// 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 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()
}
first 和 second 项的小部件 URL 似乎被第三项覆盖,所有深度 link 将打开第三项的内容。
为从 ForEach 生成的视图添加深度 link 的正确方法是什么?
这里是接口契约(标注处注意)
/// 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
}
}
}