Fra*_*ank 3 arrays ios widgetkit swiftui
我在 iOS 14 中型小部件中显示 3 行,如下所示:
row 1
-------
row 2
-------
row 3
-------
Run Code Online (Sandbox Code Playgroud)
我的视图结构在这里:
VStack {
ForEach(records, id: \.id) { item in
ZStack {
// some views
}
.widgetURL(URL(string: "wig://\(item.id)"))
}
Divider()
}
Run Code Online (Sandbox Code Playgroud)
似乎第一项和第二项的小部件 URL被第三项覆盖,所有深层链接都将打开第三项的内容。
为 ForEach 生成的视图添加深层链接的正确方法是什么?
这里是接口契约(注意标记处)
/// 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
Run Code Online (Sandbox Code Playgroud)
相反,您必须使用Link,例如
ForEach(records, id: \.id) { item in
Link(destination: URL(string: "wig://\(item.id)")!) {
ZStack {
// some views
}
}
}
Run Code Online (Sandbox Code Playgroud)