【问题标题】:SwiftUI TabView More tab crashes first pressSwiftUI TabView 更多选项卡崩溃第一次按下
【发布时间】:2021-07-05 20:54:24
【问题描述】:
var body: some View {
        TabView {

            ScheduleView().tabItem {
                Image(systemName: "calendar")
                Text("Schedule")
            }.tag(tbScheduleTag)
            DutyBookView().tabItem {
                Image(systemName: "books.vertical")
                Text(dutyBookViewTabText)
            }.tag(tbDutyBookTag)
            TimetableView().tabItem {
                Image(systemName: "calendar.badge.clock")
                Text("Timetable")
            }.tag(tbTimetableTag)
            **... plus 7 other Tabs removed for post readability** 
         }
    }

我的 TabView 创建了 10 个选项卡,因此我自动获得了“更多”TabItem。应用程序启动后,我在其他任何选项卡崩溃之前按该更多选项卡,然后返回到初始选项卡。如果我先点击任何其他选项卡或再次点击更多选项卡,它会正常加载。演示见附件。第一次选择它会在第二次工作时崩溃。

请大家给点建议?

【问题讨论】:

    标签: swiftui ios14 xcode12 tabview tabitem


    【解决方案1】:

    我添加了@State 变量并将其设置为TabViewselection 属性。为了完成这项工作,我将您的标签替换为新的 enum 值。

    struct ContentView: View {
        enum Tab {
            case schedule, dutyBook, timetable, locateTrain, settings
            case tfLRestricted, subscription, serviceStatus, info, storedDuties
        }
        
        @State var tab: Tab = .schedule
        
        var body: some View {
            TabView(selection: self.$tab) { //this is the solution
                ScheduleView().tabItem {
                    Image(systemName: "calendar")
                    Text("Schedule")
                }.tag(Tab.schedule)
                
                DutyBookView().tabItem {
                    Image(systemName: "books.vertical")
                    Text(dutyBookViewTabText)
                }.tag(Tab.dutyBook)
                
                // ...
            }
        }
    }
    

    【讨论】:

    • 只需添加 (selection: self.$tab) 即可。我省略了它,因为我认为 id 只需要以编程方式设置选定的选项卡。谢谢@Pyry Lahtinen
    猜你喜欢
    • 2020-07-12
    • 2021-04-24
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2021-05-18
    相关资源
    最近更新 更多