我是iOS开发的新手。对于我的小使者,我需要一个联系人选择器和一个文本区域。因此TTMessageController of the Three20 project似乎非常有趣。

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

我已经成功设置了three20。但是我该如何使用呢?我可以通过界面生成器还是仅通过代码使用它?在我的情况下将采取什么方法?

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

最佳答案

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

编辑:如果此时您仅具有视图控制器的框架,则最好将MessageTestController克隆到您的应用程序中并对其进行调整,而不是尝试在控制器中重新实现它的位。该示例应用程序不执行的操作之一是将MessageController挂接到您的地址簿。为此,您需要创建一个AddressbookModel和AddressBookModelDataSource,如下所示:

地址簿数据源.h

#import <Three20/Three20.h>

@class AddressBookModel;

@interface AddressBookDataSource : TTSectionedDataSource {
    AddressBookModel* _addressBook;
}

@property(nonatomic,readonly) AddressBookModel* addressBook;

@end


地址簿数据源

#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


地址簿模型

#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


通讯录中的内容是整个练习中最困难的部分。剩下的真的很容易。

08-04 22:07