UIPreferencesTable Made (Relatively) Easy + Segmented Control
So, when I sat down last night to rewrite the way the “Add Account” feature in MobileChat was done, I realized how absolutely terrible we were handling the Preference table. There was no “straight forward” way to UIPreferencesTable’s and further more, no simple way to implemented segmented controls into it.
So, I wrote a few classes and a helper to take care of it for me. Here’s a run down:
* UIPreferencesTableGroup — Quickly create a group of cells
* UIPreferencesSegmentedTableGroup — Extends UIPreferencesTableGroup, but adds support for Segments
* UIPreferencesController — This is the dataSource/delegate of any UIPreferenceTalbe you create.
* UIPrefHelper — You can use this even without the above classes, just contains a few quick make functions to create text fields.
Here’s how it works in action:
#import <UIKit/UIKit.h>
#import "UIPrefHelper.h"
#import "UIPreferencesSegmentedTableGroup.h"
#import "UIPreferencesController.h"
@interface TestApp : UIView {
UIPreferencesTable* _prefsTable;
UIPreferencesTableGroup* _groupOne;
UIPreferencesSegmentedTableGroup* _groupTwo;
UIPreferencesController* _prefs;
}
- (void) createTable;
- (void) getValues;
@end
#import "TestApp.h"
@implementation TestApp {
- (void) createTable {
_prefsTable = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
_prefs = [[UIPreferencesController alloc] init];
_groupOne = [UIPreferencesTableGroup groupWithLocalizedTitle:@”TestApp Preferences” icon:[UIImage applicationImageNamed: @”test.png”]];
_groupOne.titleHeight = 65.0f;
_groupOne.tag = 1;
[_groupOne addTextField: @”Option 1″ keyboard: UIKBDefaultQWERTYBlack autocaps: NO];
[_groupOne addTextField: @”Option 2″ keyboard: UIKBSpartanBlack autocaps: NO];
_groupTwo = [UIPreferencesSegmentedTableGroup segmentedGroup];
[_groupTwo setSegmentedControlWithTable:_prefsTable titles:@”Server”, @”Account”, nil];
[_groupTwo addTextField:@”User Name” keyboard:UIKBEmailSpace autocaps:NO];
[_groupTwo addSecureTextField:@”Password”];
[_groupTwo nextSegmentPanel];
[_groupTwo addTextField:@”Server” keyboard:UIKBURL autocaps:NO initial: @”www.google.com”];
[_groupTwo addTextField:@”Port” keyboard:UIKBSpartanBlack autocaps:NO initial: @”80″];
[_groupTwo nextSegmentPanel];
[_groupTwo setSegmentPanel:0];
[_prefs addGroup: _groupOne];
[_prefs addGroup: _groupTwo];
[_prefsTable setDataSource: _prefs];
[_prefsTable setDelegate: _prefs];
[_prefsTable setPadding: 10.0f];
[self addSubview: _prefsTable];
[_prefsTable reloadData];
}
- (void) getValues {
NSString* opt1 = [[[_groupOne row: 0] textField] text];
NSString* opt2 = [[[_groupOne row: 1] textField] text];
NSString* user = [[[_groupTwo row: 0 segment: 0] textField] text];
NSString* pass = [[[_groupTwo row: 1 segment: 0] textField] text];
NSString* server = [[[_groupTwo row: 0 segment: 1] textField] text];
NSString* port = [[[_groupTwo row: 1 segment: 1] textField] text];
}
@end
You also need the UIKeyboardType enum from MobileChat:
typedef unsigned UIKeyboardType;
enum {
UIKBDefaultQWERTY=0,
UIKBNumericPunc=1,
UIKBPhone=2,
UIKBURL=3,
UIKBSMS=4,
UIKBDefaultQWERTYBlack=5,
UIKBSpartan=6,
UIKBSpartanBlack=7,
UIKBEmailSpace=8,
UIKBEmailAt=9
};
Note: I wrote the example code inside of wordpress, so if it’s not the right, or the syntax isn’t accurate, my apologies.
I’m sure if you got to a point where you actually need this, you’ll figure out any errors that might be above.
You can grab the code here: http://mt12.quickshareit.com/share/uipreferencescontrol4c6dd.zip
Also check out the MobileChat source code to see a real example: http://trac.mobilechat.twenty08.com:2008/browser/trunk/Source/Views/SignOn
It still takes forever to “request authorization” when using it even over Wi-Fi.