现在的位置: 首页 > 综合 > 正文

320学习笔记 1

2012年11月12日 ⁄ 综合 ⁄ 共 3628字 ⁄ 字号 评论关闭

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

20110517

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

performSelector:

- (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

抱歉!评论已关闭.