Three20中TTNavigator傳參

簡單映射:
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]]; web

上面的代碼就是給頁面註冊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; 
} app

爲何會調用這個方法呢? 由於咱們在上面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; 
} this

注意:若是
- (id)initWithMenu:(MenuPage)page 
這個方法與上面多參數方法同時存在的話, 那麼首先是調用上面這個單參數方法,並無智能的根據參數的不一樣而選擇初始化方法,避免錯誤調用方法就是在映射的時候將方法名命名爲不一樣的名字。 url

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]; get

那麼點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]]; 數學

好了,就些就是我學習的一點點小結,可能有誤 ,望你們指正。 it

相關文章
相關標籤/搜索