我正在尝试创建一个循环
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];
}
}
有几个方法可以帮你解决这个问题…
首先,忘记给视图控制器一个随机名称的强引用的想法,这是没有必要的。
UIView有一个tag属性,这是用来标识视图的。在创建视图并将其添加到数组时,将标记属性分配给视图。然后,您可以通过对数组进行过滤或遍历数组,再次获得知道标签编号的视图。
另一种方法是使用字典,其中键是视图的编号,值是视图本身。因此,通过这种方式,您可以通过使用键获取值来获得视图。
另一个,数组是有序的,当你向其中添加项目时,顺序保持不变,除非你显式地修改它,将一个项目添加到可变数组将把该项目放在最后,所以本质上你知道哪个视图是哪个,因为你知道何时将它们添加到数组。
您不能这样做,但是可以做的是将引用存储在字典中,并在其他地方使用,如:
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];
}