2011-12-02 120 views
0

我試圖創建一個循環動態創建循環

for (int i = 0; i < [tabNumbers count]; i++) 
{ 
    UIViewController *viewController; 
    viewController = [[UIViewController alloc] init]; 
    viewController.title = [tabNumbers objectAtIndex:i]; 
    viewController = [tabNumbers objectAtIndex:1]; 

    [viewControllersList addObject:viewController]; 
    [viewController release]; 
} 

變量,但問題是,當我添加查看陣列我得到隨機的名字一堆視圖控制器。

不是

UIViewController *viewController; 

我試圖做這樣的事情

UIViewControlller *[NSString stringWithFormat:@"%@Controller",[tabNumbers objectAtIndex:i]] 

但這不起作用

其原因是,是,一旦我創建的意見在循環中,我正在尋找創建一個標籤欄與每個視圖作爲選項卡,然後每個選項卡調用其自己的視圖控制器

我現在不能調用視圖控制器,因爲我不知道每次會產生什麼樣的隨機數。

感謝

鮑勃

哇 - 兩個夢幻般的和及時的反應。

不幸的是,我對Xcode很新穎,並且在吸收方面很慢。我想如果我發佈完整的代碼,它可能有助於解釋我正在嘗試做什麼。

這樣的想法是,用戶可以添加,刪除,移動或重命名標籤欄項目,因爲他們認爲合適的。

,我得的一點是,我可以添加標籤欄項目,但我無法獲取標籤欄項目來創建新表。

我知道代碼是正確的,使標籤欄項目 - 我可以添加儘可能多的標籤欄項目,我想要的。 我知道,如果我只繪製一個標籤欄項目,我可以繪製表格

但我不能做的是獲取多個標籤欄項目,每個繪製一個表格。

我認爲最好的辦法是動態命名視圖控制器,這你們已經證明了我是不可能的。

所以在下以爲是試圖從陣列得到它,但考慮到該陣列也將取決於什麼用戶使標籤欄名稱改變,我看不出這是如何工作的。

有什麼建議嗎?

如有可能,包括一些代碼片段 - 我開始理解代碼,當我讀到它,但我不明白,解釋的定義(如對於視圖控制器標籤)

感謝

鮑勃

- (void)showTabBar 
{ 
GuidelinesAppDelegate *AppDelegate = (GuidelinesAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSMutableArray *viewControllersList = [[NSMutableArray alloc] init]; 
NSMutableDictionary *tabItemsDict = [[NSMutableDictionary alloc] initWithObjects:tabNumbers forKeys:tabNumbers]; 
NSString *Path = [[NSBundle mainBundle] bundlePath]; 
NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"]; 

UIViewController *viewController; 
for (int i = 0; i < [tabNumbers count]; i++) 
{ 
    viewController = [[UIViewController alloc] init]; 
    viewController.title = [tabNumbers objectAtIndex:i]; 
    [viewControllersList addObject:viewController]; 
    [viewController release]; 
} 

AppDelegate.tabItemsDict = tabItemsDict; 
[self.tabBarController setViewControllers:viewControllersList animated:YES]; 

for (NSString *s in viewControllersList) 
{ 
    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];   
    NSMutableDictionary *tabTableStructureDict = [[NSMutableDictionary alloc] initWithContentsOfFile:DataPath]; 
    AppDelegate.tabTableStructureDict = tabTableStructureDict; 

    UINavigationController *bNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 
    self.navigationController = bNavigationController; 
    [self.view addSubview:[navigationController view]]; 

    [bNavigationController release]; 
    [rootViewController release]; 
} 

}

回答

0

幾種方法可能解決這個你...

首先,忘記給視圖控制器以任意名稱很強的參考,這是沒有必要的想法。

的UIView有一個標記屬性,這是指用於識別意見。在創建視圖時將標記屬性分配給視圖並將其添加到數組中。然後,您可以通過在數組上進行過濾或遍歷它來了解標籤編號,從而再次獲取該視圖。

另一種方法是使用字典在您的關鍵是視圖的數量和價值的觀點本身。這樣你可以通過使用鍵獲取值來獲得視圖。

另外,數組是按順序排列的,因爲您向它們添加項目時,順序保持不變,除非您明確修改它,向可變數組中添加項目會將該項目放在最後,因此固有地知道哪個視圖是哪個因爲您知道何時將它們添加到陣列中。

+0

所以,我我收到有關不能夠做什麼,我想要的意見後,我幾乎放棄了什麼,我試圖做的,去和重組從一開始的項目。謝謝你的幫助 –

0

你不能這樣做,但你可以是存儲在引用什麼字典,並在其他地方使用,如下所示:

NSMutableDictionary *controllers = [NSMutableDictionary dictionary]; 

for (int i = 0; i < [tabNumbers count]; i++) 
{ 
    UIViewController *viewController = [[UIViewController alloc] init]; 
    viewController.title = [tabNumbers objectAtIndex:i]; 
    [controllers setValue:viewController forKey:[NSNumber numberWithInt:i]; 
    [viewController release]; 
} 
+0

所以,我我收到有關不能夠做什麼,我想要的意見後,我幾乎放棄了什麼,我試圖做的,去和重組從一開始的項目。感謝您的幫助 –