This blog has moved!

Please follow the twenty08 blog on tumblr! We'll have new updates on MobileChat, QuickShareIt, and other products there!

http://tumblr.twenty08.com

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

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.



Other Posts
MobileChat v2.21 Available: Localization, UTF8, and Themes!
Temporary Installer.app Repository for MobileChat

Reader Comments

It still takes forever to “request authorization” when using it even over Wi-Fi.

Does this mean MSN works now on mobilechat or still broken?

Guys, come on.

We’ve setup both a forum and a trac to handle MobileChat issues.

Please keep MobileChat issues out of other blog posts.

hey shaun,
wat is new with ver 2.21
thanks

Hello Shaun,

So did you use my localizable.strings french localization? Are you gonna release an early beta or something? :-)
Please let us know :-)

Hey Shaun i looked around before posting for a answers, I just updated mobile chat v.2.21 and it works awesome thanks for fixing the ipod touch problems. I can log on fine using my msn account but i get a error when i type a message saying it could not be sent. Is there a easy fix to this?

I Presume from reading all these comments that mobile chat DOES NOT work with firmware 1.1.4 since when I go add account nothing appears in accounts window after selecting MSN…….will be great when it does!!!!!