Fork me on GitHub

Three20库研究

虽然很早就知道Three20这个强大的库,可是一直没有学习怎么用。个人觉得学习最好的方式之一就是记笔记,一来方便以后的温故,二来也可以让人“挑刺”达到交流的目的。最后么,希望通过blog这个平台,可以监督自己的学习进程。

20110517

学习的时候首先关注了TTCore(看到了core,所以就先从这个下手了)中对cocoa类的category。比如说NSString,就有判断空格换行,解析url请求,生成MD5等等。还有NSDate,有很多格式化时间输出字符串的方法,都很实用。这些category都很容易看懂,使用时从名字能方便的看出效果。在这些方法中,有个函数比较陌生,我把注释一并写在下面。

- (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3 {
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
//记录方法的参数和返回值类型,通常用于对象间传递消息;通常随后会创建NSInvocation对象,来调用方法
if (sig) {
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];//对象间信息存储和传递
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];//注意索引号
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo invoke];//调用函数,判断是否有返回值
if (sig.methodReturnLength) {
id anObject;
[invo getReturnValue:&anObject];
return anObject;

} else {
return nil;
}

} else {
return nil;
}
}



做ios的大多要和navigationController,tabBarController还有tableviewController打交道,那就得看看Three20是如何操作的。在此之前我们需要了解基于url的navigator。TTNavigator就是将url形式的字符串通过map隐射到响应的类,当需要某个类的时候,只需要生成负荷映射规则的url即可。

TTNavigator* navigator = [TTNavigator navigator];
navigator.window
= window;

TTURLMap
* map = navigator.URLMap;
[map from:
@"tt://restaurant/(initWithName:)"
toViewController:[RestaurantController
class]];

要调用RestaurantController的时候,只需要生成URL,如:@"tt://restaurant/Chinese",就可以调用。

 

[navigator openURLAction:[TTURLAction actionWithURLPath:@"http://github.com/jverkoey"]];
[[TTNavigator navigator] openURLAction:
[[TTURLAction actionWithURLPath:
@"tt://restaurant/Chotchkie's"] applyAnimated:YES]]

 

TTNavigator的另一个巨大的优点,就是持久化。举个使用tabBarController的例子,如果你关闭程序前在tab3页面上,那么你再次打开的时候还是在tab3页面上。当你使用URL的时候,TTNavigator会记住URL的调用栈,下次再使用的时候,只有最近的一个viewController会被初始化。由于所有的url都记录下来了,所以你也不需要担心,如果我需要回到上一级页面时怎么back。TTNavigator会根据URL按需实例化,这样就大大节省了内存。

 

接下来,我们看一下,URL是如何映射到类的。先看一个映射:

 

[map from:@"tt://menu/(initWithMenu:)/(page:)"
toSharedViewController:[MenuController
class]];

 

那么使用@"tt://menu/1/5"就相当于调用:

 

[[MenuController alloc] initWithMenu:1 page:5]

而320中的tableViewController是TTTableViewController,那有很多优异特性。一个可能的优点可能是省去了很多datasource和delegate的实现代码。它生成dataSource的方式很简单,比如:

 

self.dataSource = [TTSectionedDataSource dataSourceWithObjects:@"A",
[TTTableTextItem itemWithText:
@"Alen" URL:@"tt://myDetail/a"],
[TTTableTextItem itemWithText:
@"Alva" URL:@"tt://myDetail/b"],
nil];
//还有TTListDataSource,按需使用

 

知道了以上这些规则,我们基本上可以写一个navigationController或者tabBarController的例子了(先拿这个练练手,主要是熟悉URL的写法规则)。

 

- (void)applicationDidFinishLaunching:(UIApplication*)application
{
TTNavigator
* navigator = [TTNavigator navigator];
//选择persistence模式
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
navigator.window
= [[[UIWindow alloc] initWithFrame:TTScreenBounds()] autorelease];
TTURLMap
*map = navigator.URLMap;
// Any URL that doesn't match will fall back on this one, and open in the web browser
[map from:@"*" toViewController:[TTWebController class]];

// The tab bar controller is shared, meaning there will only ever be one created. Loading
// This URL will make the existing tab bar controller appear if it was not visible.
[map from:@"tt://tabBar" toSharedViewController:[MyTabController class]];
[map from:
@"tt://menu/(initWithMenu:)" toSharedViewController:[MenuController class]];
// [map from:@"tt://myMenu/(initWithNumber:)" toSharedViewController:[MenuController class]];

[map from:
@"tt://myDetail/(loadFromNib:)" toViewController:[MyDetailViewController class]];

if (![navigator restoreViewControllers]) {
// This is the first launch, so we just start with the tab bar
[navigator openURLAction:[TTURLAction actionWithURLPath:@"tt://tabBar"]];
}
}

 

最后需要注意的是,一定要将原来的

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 

注释掉,因为似乎applicationDidFinishLaunching:的优先级没有它高。

更多信息请参考Three20Info

iPhone中添加Three20的方法

iPhone应用开发中关于Three20添加方法是本文要介绍的内容,主要是来学习iphone应用中的Three20的相关操作,具体内容来看本文详解。Three20是一个iPhone开发图形接口类的集合,包含图片查看,一般工具,HTTP本地缓存。Three20是来自于 Facebook的iPhone程序源代码的。

添加Three20到您的程序

Three20是预编译静态库,最简单的方法是使用Xcode的依赖工程特性,下面是方法:

1、复制(Clone命令)three20 git(GitHub) 目录: `git clone git://github.com/joehewitt/three20.git`.  确保将其储存在一个常用位置,以便Xcode每次编译时来索引和访问

2、在 “three20/src”中找到 “Three20.xcodeproj” . 拖拽Three20.xcodeproj到您工程的根目录”Groups and Files”边栏中.在弹出对话框中去掉”Copy items”的对号,”Reference Type”选择”Relative to Project”然后点“Add”.

3、现在要链接Three20库到您的工程。点击添加进来的”Three20.xcodeproj”,在“Details”表中,你会看到 libThree20.a. 打上文件右侧的对号

4、现在你需要设置320库为你程序的依赖库,以便Xcode在编译的时候也会将320一起编译起来。展开左侧的“Targets”标签,双击您程 序的“目 标”文件。在“General”标签下,你会看到“Direct Dependencies”,点“+”号添加320库。

5、现在你需要添加图片和字符串包到你的程序,找到”Three20.bundle”在”three20/src”中,然后拖拽到你的程序中。对话框里面 “Copy items”别选,“reference type”选“relative to project”

6、现在,你需要添加 Core Animation framework到你的工程。点击程序target,然后在frameworks里面添加 QuartzCore.framework就可以了(这个不是翻译的,原来的方法太麻烦了

7、最后,我们需要让你的工程找得到320库的文件头。打开 “Project Settings”找到”Build”标签,找到 “Header Search Paths”双击它。添加到”three20/src”相对目录的地址。

8、当你在 Project Settings, 找到 “Other Linker Flags”在”Linker”区,添加”-ObjC”和”-all_load”。

9、OK,在用320库的地方添加 #import “Three20/Three20.h” 就好了。

小结:iPhone应用开发中关于Three20添加方法的内容介绍完了,希望通过本文的学习能对你有所帮助!

原文:http://mobile.51cto.com/iphone-284942.htm

posted on 2012-02-16 19:24  pengyingh  阅读(604)  评论(0编辑  收藏  举报

导航