快速构建App设置界面的框架:Bohr

jopen 9年前

快速构建App设置界面的框架:Bohr

Bohr让你能够为你的App构建一个设置界面。具有:简便、可定制和扩展性的特点。

快速构建App设置界面的框架:Bohr

By default, Bohr supports multiple setting types such as strings, booleans or times. However, this framework has been built focusing a lot of attention in extensibility, meaning you can build your own custom classes to support any kind of setting type you want.

Why "Bohr"?

"Bohr" comes from Neils Bohr, conceiver of an atomic model which introduces the concept of electronic configuration, a way to organize electrons by layers around the atom nucleus.

True story.

Installation

Carthage

github "DavdRoman/Bohr"

CocoaPods

pod 'Bohr'

Manual

Drag and copy all files in the Bohr folder into your project.

At a glance

Setup

The settings screen you're going to set up is represented by aUITableViewControllersubclass calledBOTableViewController. Such controller managesBOTableViewSectioninstances, which manages a set ofBOTableViewCellinstances.

All you need to start building your settings screen is aBOTableViewController(or subclass) instance, which you'll initialize:

BOTableViewController *tableViewController = [[BOTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

Note: you may useUITableViewStylePlaintoo, but keep in mind most apps useUITableViewStyleGrouped.

Next, we'll add a section to our view controller:

BOTableViewSection *section = [BOTableViewSection sectionWithTitle:@"My section"];  [tableViewController addSections:@[section]];

Note: you may add your sections and cells in a subclass ofBOTableViewController. In case you decide to do so, there's asetupmethod where you can, well... do the setup.

And finally, we'll add some cells to our section (for the sake of simplicity, I'll add just one):

BOSwitchTableViewCell *switchCell = [BOSwitchTableViewCell cellWithTitle:@"Switch option" setting:[BOSetting settingWithDefaultValue:@YES forKey:@"key_for_bool_option"]];  [section addCells:@[switchCell]];

Note:BOSettingis a simple object that defines the setting the cell will represent through akeyand avalueproperty. It also takes care of some business to make theNSUserDefaultslogic layer easier to manage, but you don't need to worry about that at all.

Built-in BOTableViewCell's

As mentioned before, there's a bunch of built-in BOTableViewCell subclasses ready to be used:

  • BOSwitchTableViewCell: managesBOOLvalues through aUISwitchcontrol.
  • BOTextTableViewCell: managesNSStringvalues through aUITextFieldcontrol.
  • BOChoiceTableViewCell: managesNSIntegervalues (which you can understand as "options" from aNS_ENUM) through taps on the cell itself.
  • BOTimeTableViewCell: managesNSIntegervalues that represent a given time as the minute interval from midnight to such time. A revealingUIPickerViewis used to set the time.
  • BODisclosureTableViewCell: this cell is used to pushes a view controller from the current one. Note yourBOTableViewControllerneeds to be embedded in aUINavigationControllerin order for this cell to work.
  • BOButtonTableViewCell: allows the user to perform an action when the cell is tapped.

Again, this framework is all about extensibility, so if you want to provide your own implementation for any kind of setting type you can. Read below to know how.

Subclassing BOTableViewCell

Creating a subclass of BOTableViewCell is fairly straighforward.

First of all, the framework contains a header file calledBOTableViewCell+Subclass.h. You must import that header in your subclass implementation file. That way, you'll be able to access all the necessary methods for you to implement them in your subclass:

  • setup: used to set up the cell for the first time.
  • updateAppearance: any code that defines the appearance of the cell should be put here. Note everyBOTableViewCellinstance contains 4 properties used to define its style:
    • mainColor: the color used for the title and other main elements of the cell.
    • mainFont: the font used for the title and other main elements of the cell.
    • secondaryColor: the color used for the detail text and other secondary elements of the cell.
    • secondaryFont: the font used for the detail text and other secondary elements of the cell.
  • wasSelectedFromViewController:: called when the cell is tapped. TheBOTableViewControllerwhere the cell is contained is passed.
  • settingValueDidChange: called when theNSUserDefaultvalue associated with the cell changes. You must represent such change in the visual element of your cell. Accessing that new value is as simple as callingself.setting.value.

There's also a property calledexpansionHeight. If you set that property (preferably insetup), the cell height will be expanded when tapped. That extra height is determined byexpansionHeightitself. As a side note, you shouldn't overridewasSelectedFromViewController:if you setexpansionHeight, and if you do, please make sure to always callsuper.

Please take a look to the implementation code ofBOSwitchTableViewCellfor a more detailed demonstration on how to subclassBOTableViewCell.

项目主页:http://www.open-open.com/lib/view/home/1435129255403