如何使用Three20 TTMessageController?

时间:2011-03-21 06:58:15

标签: iphone objective-c three20

我是iOS开发的新手。对于我的小信使,我需要一个联系人选择器和一个textarea。所以TTMessageController of the Three20 project似乎非常有趣。

但是我不确定如何实现它。现在我有三个控制器,每个视图一个。我希望在第三个视图中有一个联系人选择器和textarea。

我已成功设置了three20。但是我该如何使用它?我可以通过界面构建​​器或仅通过代码使用它吗?在我的案例中会采取什么方法?

在开始这项工作之前,我想确保这是正确的解决方案。 three20是否允许我决定如何处理来自文本字段的输入?我想用自己的网关发送短信。

1 个答案:

答案 0 :(得分:6)

查看three20源附带的TTCatalog示例应用程序的源代码。它有一个调用TTMessageController和处理字段的例子。基本上,您在类中实现TTMessageControllerDelegate,并且一旦按下发送按钮,TTMessageController将从消息中将字段发送给您进行处理。我正在使用这个类作为前端,通过我的应用程序中的第三方网关发送SMS消息。我将它与消息气泡视图相结合,以模仿本机SMS应用程序,它就像一个冠军。

编辑:如果你现在只有一个视图控制器的骨架,你可能最好将MessageTestController克隆到你的应用程序并调整它,而不是试图在你的控制器中重新实现它的一部分。示例应用程序没有做的一件事是将MessageController挂钩到您的地址簿。为此你需要像这样创建一个AddressbookModel和AddressBookModelDataSource:

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

地址簿的内容是整个练习中最难的部分。其余的很容易。