Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

iphone - How to use Three20 TTMessageController?

I am new to iOS development. For my little messenger I need a contact picker and a textarea. So the TTMessageController of the Three20 project seems to be very interesting.

However I am not really sure how to implement it. For now I have three controllers, one for each view. I want to have a contact picker and textarea on the third view.

I have set up three20 successfully. But how do I use it? Can I use it through interface builder or just by code? What would be an approach in my case?

Before I get started on this I want to be sure that this is the right solution. Is it true that three20 lets me decide how to deal with the input that comes from the text fields? I want to send sms with my own gateway.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Look at the source code for the TTCatalog example app that comes with the three20 source. It has an example of calling TTMessageController and handling the fields. Basically you implement the TTMessageControllerDelegate in your class and the TTMessageController will send the fields from the message to you for processing once the send button is pressed. I'm using this class as a front end for sending SMS messages via 3rd party gateway in my app. I've combined it with a message bubble view to mimic the native SMS application and it works like a champ.

EDIT: If you just have a skeleton of a view controller at this point you might be better off cloning MessageTestController into your app and adapting it rather than trying to reimplement bits of it in your controller. One thing the sample app doesn't do is hook the MessageController to your addressbook. For that you'll need to create a AddressbookModel and AddressBookModelDataSource like this:

AddressbookDataSource.h

#import <Three20/Three20.h>

@class AddressBookModel;

@interface AddressBookDataSource : TTSectionedDataSource {
    AddressBookModel* _addressBook;
}

@property(nonatomic,readonly) AddressBookModel* addressBook;

@end

AddressbookDataSource.m

#import <AddressBookUI/AddressBookUI.h>

#import "AddressBookDataSource.h"
#import "AddressBookModel.h"

@implementation AddressBookDataSource

@synthesize addressBook = _addressBook;

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)init {
    if (self = [super init]) {
        _addressBook = [AddressBookModel new];
        self.model = _addressBook;
    }
    return self;
}

- (void)dealloc {
    RELEASE_SAFELY(_addressBook);
    RELEASE_SAFELY(self.items);

    [super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewDataSource


- (void)tableViewDidLoadModel:(UITableView*)tableView {

    RELEASE_SAFELY(self.items);

    self.items = [NSMutableArray new];
    int countPeople = [((AddressBookModel *)self.model).searchResults count];

    for (int i = 0; i < countPeople; i++) {
        ABRecordRef person = [((AddressBookModel*)self.model).searchResults objectAtIndex:i];
        ABMultiValueRef phoneNumberMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
        NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberMultiValueRef);
        RELEASE_CF_SAFELY(phoneNumberMultiValueRef);

        if ([phoneNumbers count]) {
            NSString *personName = (NSString *)ABRecordCopyCompositeName(person);
            for (NSString *phoneNumber in phoneNumbers) {

                TTTableItem* item = [TTTableSubtitleItem itemWithText:personName subtitle:phoneNumber];
                [_items addObject:item];
            }
            RELEASE_SAFELY(personName);
        }
        RELEASE_SAFELY(phoneNumbers);
    }
} 

- (void)search:(NSString*)text {
    [_addressBook search:text];
}

- (NSString*)titleForLoading:(BOOL)reloading {
    return @"Searching...";
}

- (NSString*)titleForNoData {
    return @"No names found";
}

@end

AddressBookModel.h

#import <Three20/Three20.h>

@interface AddressBookModel : NSObject <TTModel> {
    NSMutableArray* _delegates;
    NSArray* _searchResults;
}

@property(nonatomic,retain) NSArray* searchResults;

- (void)search:(NSString*)text;

@end

AddressBookModel.m

#import "AddressBookModel.h"
#import <AddressBookUI/AddressBookUI.h>

@implementation AddressBookModel

@synthesize searchResults = _searchResults;


- (id)init {
    if (self = [super init]) {
        _delegates = nil;
        _searchResults = nil;
    }
    return self;
}

- (void)dealloc {
    RELEASE_SAFELY(_delegates);
    RELEASE_SAFELY(_searchResults);
    [super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTModel

- (NSMutableArray*)delegates {
    if (!_delegates) {
        _delegates = TTCreateNonRetainingArray();
    }
    return _delegates;
}

- (BOOL)isLoadingMore {
    return NO;
}

- (BOOL)isOutdated {
    return NO;
}

- (BOOL)isLoaded {
    return YES;
}

- (BOOL)isLoading {
    return NO;
}

- (BOOL)isEmpty {
    return !_searchResults.count;
}

- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
}

- (void)invalidate:(BOOL)erase {
}

- (void)cancel {
    [_delegates perform:@selector(modelDidCancelLoad:) withObject:self];
}


- (void)search:(NSString*)text {
    [self cancel];

    if (text.length) {
        [_delegates perform:@selector(modelDidStartLoad:) withObject:self];

        ABAddressBookRef addressBook = ABAddressBookCreate();

        CFStringRef searchText = CFStringCreateWithCString(NULL, [text cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);
        self.searchResults = (NSArray*) ABAddressBookCopyPeopleWithName(addressBook, searchText);

        RELEASE_CF_SAFELY(searchText);

        [_delegates perform:@selector(modelDidFinishLoad:) withObject:self];

        RELEASE_CF_SAFELY(addressBook);
    } else {
        self.searchResults = nil;
    }
    [_delegates perform:@selector(modelDidChange:) withObject:self];
} 

@end

The addressbook stuff was seriously the hardest part of the whole exercise. The rest is really easy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...