测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上一个版本 | 2016年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Tomasz Gebarowski 维护。
SwiftySettings 是一个用于构建和展示应用设置的 Swift 库。它可以帮助声明复杂的设置树,提供了一个协议来存储和检索数据,并支持使用 UISplitViewController 的 iPhone 和 iPad 用户界面。
使用 SwiftySettings,您可以声明一个复杂的设置树,将其映射到用户界面,并与其持久层集成。下面的用户界面可以使用以下代码片段创建。
func loadSettingsTopDown() {
/* Top Down settings */
settings = SwiftySettings(storage: storage, title: "Intelligent Home") {
[Section(title: "Electricity") {
[OptionsButton(key: "tariff", title: "Tariff") {
[Option(title: "Day", optionId: 1),
Option(title: "Night", optionId: 2),
Option(title: "Mixed", optionId: 3)]
},
Switch(key: "light-central", title: "Central Switch", icon: UIImage(named: "settings-light")),
Screen(title: "Livingroom") {
[Section(title: "Lights") {
[Switch(key: "light1", title: "Light 1"),
Switch(key: "light2", title: "Light 2"),
Slider(key: "brightness-1", title: "Brightness",
minimumValueImage: UIImage(named: "slider-darker"),
maximumValueImage: UIImage(named: "slider-brighter"),
minimumValue: 0,
maximumValue: 100)]
}]
},
Screen(title: "Bedroom") {
[Section(title: "Lights", footer: "Manage lights in your bedroom") {
[Switch(key: "light3", title: "Light 1"),
Switch(key: "light4", title: "Light 2"),
Slider(key: "brightness-2", title: "Brightness")]
}]
}]
},
OptionsSection(key: "alarm-status", title: "Alarm") {
[Option(title: "Armed", optionId: 1),
Option(title: "Only ground floor", optionId: 2),
Option(title: "Disarmed", optionId: 3)]
}
]
}
}
为了加载和保存设置,必须定义一个实施 SettingsStorageType 协议的存储类。
public protocol SettingsStorageType {
subscript(key: String) -> Bool? { get set }
subscript(key: String) -> Float? { get set }
subscript(key: String) -> Int? { get set }
subscript(key: String) -> String? { get set }
}
class ExampleSettingsController: SwiftySettingsViewController {
var storage = Storage()
...
override func viewDidLoad() {
super.viewDidLoad()
settings = SwiftySettings(storage: storage, title: "Main Screen Name") {
[Section(title: "Section Name") {
[Switch(key: "key", title: "Switch",
icon: UIImage(named: "settings-1")]
}
})
}
}
注意:SwiftySettings 对象负责声明设置树。上面的代码片段声明了一个名为“Main Screen Name”的 TEDTableViewcontroller,具有名为“Section Name”的分区和一个标题和图标的 UISwitch 设置。SwiftySettings 对象使用 Storage 对象来加载和保存设置状态。
SwiftySettings 允许表示一个复杂的多层设置树。如图所示,这类树的根是 Screen 对象。Screen 作为单个 UITableViewDataSource 中所有设置的容器。它使用 Section 和 Option Section 元素数组进行初始化。每个 Section 和 Option Section 都映射到 UITableViewDataSource 的 Header 或 Footer。Section 对象可以包含开关、滑块或选项按钮设置,而 Option Section 只包含选项元素。此外,Section 还可以包含其他 Screen 对象,从而引入新的设置导航级别。
开关代表一个包含 UISwitch、UILabel 和可选图标的 UITableViewCell。开关元素只能添加到 Section 中,并且可以如下初始化:
init(key: String,
title: String,
defaultValue: Bool = false,
icon: UIImage? = nil,
valueChangedClosure: ValueChanged? = nil)
参数
滑块代表一个包含 UISlider、UILabels 和可选图标的 UITableViewCell。滑块元素只能添加到 Section 中,并可以如下初始化
init(key: String,
title: String,
defaultValue: Float = 0,
icon: UIImage? = nil,
minimumValueImage: UIImage? = nil,
maximumValueImage: UIImage? = nil,
minimumValue: Float = 0,
maximumValue: Float = 100,
valueChangedClosure: ValueChanged? = nil)
参数
OptionsButton 代表一个包含两个 UILabel 的 UITableViewCell,一个用于设置标题,另一个用于显示当前选中的选项。当点击 OptionsButton 单元时,导航将移动到新的 UITableView,其中包含作为 OptionsButton 对象一部分的 Option 单元。如果选中了 Option,导航将返回到前一个视图。
注意:OptionButton 对象可以作为子对象添加到 Section 对象中。
init(key: String,
title: String,
icon: UIImage? = nil,
optionsClosure: (() -> [Option])? = nil)
参数
TextField 代表一个包含 UITextField(用于可编辑内容)的 UITableViewCell。
init(key: String,
title: String,
secureTextEntry: Bool = false,
defaultValue: String = "",
valueChangedClosure: ValueChanged? = nil)
参数
将 Screen 对象添加到 Section 中时,它将映射到一个包含 UILabel 的 UITableViewCell,该 UILabel 被设置为 Screen 标题。点击此类元素时,导航将移动到由 Screen 对象表示的新 UITableView,并显示所有子 Section 元素。
init(title: String,
sectionsClosure: (() -> [Section])? = nil)
节是 SwiftySettings 对 UITableViewHeaderFooter 的表示。它只能添加到 Screen 对象中。每个节应包括标题和可选页脚。
init(title: String,
footer: String? = nil,
nodesClosure: (() -> [TitledNode])? = nil)
参数
OptionsSection是一种可以直接添加到屏幕对象中的Section类型。Section和OptionsSection之间的区别在于,后者只能包含选项对象。从UI的角度看,OptionSection代表一个用具有单选选择选项的UITableViewCells填充的UITableView子区。
init(key: String,
title: String,
nodesClosure: (() -> [Option])? = nil)
参数
SwiftySettings是SwiftySettingsViewController使用的根对象。它是为您的应用程序创建设置树时的起点。
init(storage: SettingsStorageType,
title: String,
sectionsClosure: () -> [Section])
参数
注意:使用SwiftySettings对象时,会自动从标题和sectionsClosure创建第一个屏幕对象,这是为了方便,避免手动创建第一个屏幕。
可以使用Interface Builder配置SwiftySettings的外观。
SwiftySettings依照MIT许可发布。详情见LICENSE。