如何使用Three20 TTMessageController? [英] How to use Three20 TTMessageController?

查看:74
本文介绍了如何使用Three20 TTMessageController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手。对于我的小信使,我需要一个联系人选择器和一个textarea。因此, 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.

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

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.

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

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?

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

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.

推荐答案

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

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.

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

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

AddressbookDataSource.h

#import <Three20/Three20.h>

@class AddressBookModel;

@interface AddressBookDataSource : TTSectionedDataSource {
    AddressBookModel* _addressBook;
}

@property(nonatomic,readonly) AddressBookModel* addressBook;

@end

AddressbookDataSource.m

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

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

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.

这篇关于如何使用Three20 TTMessageController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆