我写的这个应用程序有问题。
我正在我的应用程序窗口中设置UITabBar
并在视图文件中设置图标。
但是当我运行应用程序时,第一个图标显示出来(因为视图已加载,我猜)并且其他图标在我点击之前不会显示。
我是否需要在self.tabBarItem
以外的其他方法中实施viewDidLoad
?
先感谢大家!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBar = [[UITabBarController alloc] init];
SubscriptionsController *subscriptionsController = [[SubscriptionsController alloc] init];
FavoritesController *favoritesController = [[FavoritesController alloc] init];
CategoriesController *categoriesController = [[CategoriesController alloc] init];
TagsController *tagsController = [[TagsController alloc] init];
HelpScreenController *helpScreenController = [[HelpScreenController alloc] init];
tabBar.viewControllers = [NSArray arrayWithObjects:
subscriptionsController,
favoritesController,
categoriesController,
tagsController,
helpScreenController,
nil
];
[window addSubview:tabBar.view];
// Override point for customization after application launch.
[window makeKeyAndVisible];
return YES;
}
//The View
- (void)viewDidLoad {
[super viewDidLoad];
tabIcon = [[UITabBarItem alloc] initWithTitle:@"Abonime" image:[UIImage imageNamed:@"subscr.png"] tag:0];
self.tabBarItem = tabIcon;
[tabIcon release];
}
答案 0 :(得分:13)
我认为您应该在视图控制器的指定初始化程序中设置tabBarItem属性(从您的代码判断,每个控制器必须为-init
)。实际上,标签栏控制器非常智能,可以按需加载视图,也就是说,应该在发送viewDidLoad
之前设置tabBarItem属性。
此外,您似乎泄漏了所有视图控制器。要解决此问题,请执行以下操作:
SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease];
答案 1 :(得分:4)
正确。图标不会显示,因为视图(第一个除了之外,尚未加载)。并且在点击视图之前不会加载,因为直到那时才调用viewDidLoad。
删除单个UIViewControllers viewDidLoad中的代码并执行此操作...
NSArray *controllers = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"SubscriptionsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"FavoritesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"CategoriesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"TagsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"HelpScreenController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
nil];
NSMutableArray *controllerArray = [NSMutableArray array] ;
for (NSUInteger i = 0; i < [controllers count]; i++)
{
id newClass = [[NSClassFromString([[controllers objectAtIndex:i] objectForKey:@"class"]) alloc] init];
UITabBarItem *tabItem = [[UITabBarItem alloc] init];
tabItem.image = [[controllers objectAtIndex:i] objectForKey:@"icon"];
tabItem.title = [[controllers objectAtIndex:i] objectForKey:@"title"];
tabItem.tag = i;
[(UIViewController*)newClass setTabBarItem:tabItem];
[tabItem release];
[controllerArray addObject:newClass];
[newClass release];
}
tabBar.viewControllers = controllerArray;