我是iOS开发的新手。在这里,我尝试使用foreach循环访问不同的目标视图
struct NewView : View
{
var arrayofviews //here I need array of views
var body:some View
{
List
{
ForEach(array)
{(item) in
NavigationLink(destination: item)
{
Text("Click Here")
}
}
}
}
}
我尝试关注
var arrayofviews=[view1(),view2(),view3()] as! [Any]
然后我收到此错误无法将类型“ view1”的值转换为预期的元素类型“ Array.ArrayLiteralElement”(又名“ AnyView”)
此处view1,view2,view3是自定义视图
struct view1:View
{
var body:Some View
{
Text("...")
...
}
}
//similarly view2 and view3 also
答案 0 :(得分:0)
struct NewView : View
{
var arrayofviews=[AnyView(view1()),AnyView(view2()),AnyView(view3())] as! [AnyView] //This helped
var body:some View
{
List
{
ForEach(0..<array.count)
{(index) in
NavigationLink(destination: array[index])
{
Text("Click Here")
}
}
}
}
}
感谢'Mohammad Rahchamani'的建议!