macOS 上的 SwiftUI 选择器
Posted
技术标签:
【中文标题】macOS 上的 SwiftUI 选择器【英文标题】:SwiftUI picker on macOS 【发布时间】:2021-01-12 14:56:42 【问题描述】:我尝试用我的个人知识来学习 SwiftUI。 然后我在我的 Mac 上进行测试,但我的第一感觉是 SwiftUI 比 macOS 更面向 ios。 我说的对吗?
现在是技术问题 我有一个数组:
var years = ["1900", "1901", "1902", "1903", "1904", "1905", "1906", "1907", "1910",
"1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", "1920",
"1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930",
"1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940",
"1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950"]
我想将这些值放入 SwiftUI 选择器中。
似乎不可能通过循环将这些值放入 Picker 中
Picker (selection: $toDate, label: Text ("From Date : "), content:
for y in years
)
总是报错:
包含控制流语句的闭包不能与函数一起使用 构建器'ViewBuilder'
【问题讨论】:
【参考方案1】:检查一下:
var years = ["1900", "1901", "1902", "1903", "1904", "1905", "1906", "1907", "1910",
"1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", "1920",
"1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930",
"1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940",
"1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950"]
@State var toDate = "1900"
var body: some View
Picker("From date:", selection: $toDate)
ForEach(years, id:\.self ) i in
Text(i)
【讨论】:
以上是关于macOS 上的 SwiftUI 选择器的主要内容,如果未能解决你的问题,请参考以下文章