动态设置SwiftUI NavigationBarItems?

时间:2020-10-10 04:14:14

标签: swiftui swiftui-navigationview swiftui-tabview

如何动态更改SwiftUI导航栏项目?

我在NavigationView中有一个TabView,我希望根据所选的选项卡更改导航栏项。但是,我很难确定如何使用.onAppear()进行更改,即使您应该这样做也是如此。

我的代码当前的布局如下:

var body: some View {
        NavigationView {
            TabView {
                contentWithNavigationButtons()
                    .tabItem {
                        Image(systemName: "house")
                        Text("Buttons")
                    }
                
                contentWithoutNavigationButtons()
                    .tabItem {
                        Image(systemName: "person")
                        Text("No Buttons")
                    }
                    .onAppear {
                        //Navigation Bar Items should be removed
                    }
                
            }
            .navigationBarItems(trailing: someButton)
        }

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案的演示-添加选项卡的跟踪选择,并根据选项卡的选择制作按钮。经过Xcode 12 / iOS 14的测试

void CMFCReadTextToBoxDlg::OnBnClickedbtnread()
{
    ifstream MyReadFile("user.txt");
    string textRead1;
    string textRead2;
    string textRead3;
    string textRead4;

    getline(MyReadFile, textRead1);
    CString cs1(textRead1.c_str());
    SetDlgItemText(txtField1, cs1);

    getline(MyReadFile, textRead2);
    CString cs2(textRead2.c_str());
    SetDlgItemText(txtField2, cs2);

    getline(MyReadFile, textRead3);
    CString cs3(textRead3.c_str());
    SetDlgItemText(txtField3, cs3);

    getline(MyReadFile, textRead4);
    CString cs4(textRead4.c_str());
    SetDlgItemText(txtField4, cs4);
    
    
    MyReadFile.close();
    
}