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

Three20中TTNavigator用法总结

2014年02月16日 ⁄ 综合 ⁄ 共 3148字 ⁄ 字号 评论关闭

简单映射:

Three20中的TNavigator对于软件导航很有用,只需要维护一张map映射表就行了。就像url表示一个网页一样,Three20也采用了相同的方式,用url关联页面。大家可以参看TTNavigatorDemo中的源码:

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:[TabBarController class]]; 


// Menu controllers are also shared - we only create one to show in each tab, so opening 

// these URLs will switch to the tab containing the menu 

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

上面的代码就是给页面注册url, 如tt:tabBar就表示TabBarController,只要调用TTOpenURL(@"tt://tabBar");这句代码就可以初始化TabBarController,并显示出来。相当于执行了TabBarController *test=[[TabBarController alloc] init]; [self.view addSubview:test.view];

如果调用TTOpenURL(@"tt://menu/1");会发生什么? 它会调用MenuController中的

- (id)initWithMenu:(MenuPage)page { 

if (self = [super init]) { 

self.page = page; 



return self; 

}

为什么会调用这个方法呢? 因为我们在上面map的时候是用的tt://menu/(initWithMenu:) 括号里是方法名,可以想像成如果有括号,那么它就表示一变量,那么它就是一对多的映射, 与数学上的映射一个道理。,tt://menu/1 tt://menu/2 tt://menu/3 ..... tt://menu/XXX 都表示这个对应, 所以TTOpenURL(@"tt://menu/2");的时候也会调用上面的方法。

而这个1,2,3 .... XXX就表未参数传入到上面这个方法。为什么会样呢,因为这是Three20的规则。还有注意,你的MenuController必须要实现

- (id)initWithMenu:(MenuPage)page 

这个方法,不然就不能达到效果。 在map映射的时候,如果加括号有方法名的时候,这个方法返回的必须是Controller. (这是我的理解,不知道正确与否,知道的大侠通知我一下)

多参数映射:

上面的情况是只有一个参数转入,如果想传多个参数如何办呢?我知道有两种方法:

1. 将映射改为:

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

这样就可以传入两个参数,相应的方法就应改为:

- (id)initWithMenu:(MenuPage)page withParam:(NSString*)param { 

if (self = [super init]) { 

self.page = page; 



return self; 



2。将映射改为:

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

这种方式也可以传入多个参数,如:TTOpenURL(@"tt://menu?menu=1&ref=hello&name=world&phone=123");这样就传入了三个参数,

那么它的初始化方法就应写为:

- (id)initWithMenu:(MenuPage)page query:(NSDictionary*)query { 


NSString *ref = [query objectForKey:@"ref"]; 

NSString *name = [query objectForKey:@"name"]; 

NSString *phone = [query objectForKey:@"phone"]; 

if (self = [super init]) { 

self.page = page; 



return self; 

}

注意:如果

- (id)initWithMenu:(MenuPage)page 

这个方法与上面多参数方法同时存在的话, 那么首先是调用上面这个单参数方法,并没有智能的根据参数的不同而选择初始化方法,避免错误调用方法就是在映射的时候将方法名命名为不同的名字。

Hash URL

在demo中有一个Hash URL的例子,它可以指定方法的调用。

如果将MenuController里的代码修改一下:

self.navigationItem.rightBarButtonItem = 

[[[UIBarButtonItem alloc] initWithTitle:@"Order" style:UIBarButtonItemStyleBordered 

target:@"tt://order?waitress=Betty&ref=toolssssstest#param" 

action:@selector(openURLFromButton:)] autorelease];

那么点order的时候就会调用ContentController里的

- (void)orderAction:(NSString*)action { 

TTDPRINT(@"ACTION: %@", action); 

}

因为map映射的是:

[map from:@"tt://order?waitress=(initWithWaitress:)" toModalViewController:[ContentController class]]; 

[map from:@"tt://order?waitresss=()#(orderAction:)" toViewController:[ContentController class]];

 

好了,就些就是我学习的一点点小结,可能有误 ,望大家指正。

参考:

http://three20.pypt.lt/url-based-navigation-and-state-persistence

http://three20.info/article/2010-10-06-URL-Based-Navigation

http://www.slideshare.net/iphonedevbr/three20 

原文链接:http://blog.csdn.net/favormm/article/details/6760533

抱歉!评论已关闭.