How to use Three20 TTMessageController?

感情迁移 提交于 2019-11-29 02:44:48

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!