【发布时间】:2020-09-18 08:46:28
【问题描述】:
今天我开始练习如何使用 UITabBarController。我有几个选项卡,但是当我看到“更多”(如果我有 7 个选项卡)时注意到,单击编辑重新排列,但是当我关闭应用程序并重新打开应用程序时,重新排列就消失了。我相信它不会保存重新排列。我有 UserDefaults.standard,但不知道如何将重新排列保存到我的 UserDefault.standard 中。我正在使用 Swift 4。这是我的代码:
func create_tab_controller() {
let first_view = UIViewController()
first_view.tabBarItem.image = UIImage(systemName: "house", withConfiguration: main_symbol_configuration)
first_view.tabBarItem.title = "Home"
first_view.view.backgroundColor = UIColor.black
let second_view = UIViewController()
second_view.tabBarItem.image = UIImage(systemName: "rectangle", withConfiguration: main_symbol_configuration)
second_view.tabBarItem.title = "Second View"
second_view.view.backgroundColor = UIColor.darkGray
let third_view = UIViewController()
third_view.tabBarItem.image = UIImage(systemName: "gear", withConfiguration: main_symbol_configuration)
third_view.tabBarItem.title = "Setting"
third_view.view.backgroundColor = UIColor.gray
main_tab_controller_order = [first_view, second_view, third_view]
main_tab_controller.viewControllers = main_tab_controller_order
view.addSubview(main_tab_controller.view)
}
【问题讨论】:
-
请用骆驼箱代替蛇箱,这样更快捷
-
我认为只有某些类型的数据可以存储在 UserDefaults 中。我会存储一个纯字符串(可能是引用选项卡类型/名称),当您获得保存的 UserDefaults 字符串时,调用一个单独的函数(可能是 switch 语句)来确定每个选项卡类型/名称应该与什么相关联。
标签: ios swift delegates uitabbarcontroller nsuserdefaults